asp.net-mvc – ASP.NET MVC将数据从视图传递给控制器

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – ASP.NET MVC将数据从视图传递给控制器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个包含添加到工作站的项目的网格的视图.用户可以从下拉列表中选择一个项目,然后单击一个动作链接,该链接调用将该项目添加到工作站的控制器.我可以通过在控件的Post操作中读取FormCollection对象来使其工作.
<p>
    <% using(Html.BeginForm("AddItem","Home")) { %>
    <label for="ItemID">Item:</label>
    <%= Html.DropDownList("ItemID",Model.ItemsList) %>
    <%= Html.Hidden("WorkstationID",Model.Workstation.RecordID) %>
    <input type="submit" value="Submit" />
    <% } %>
</p>
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddItem(FormCollection formValue)
{
    long workstationId = Convert.ToInt64(formValue["WorkstationID"]);
    long itemId = Convert.ToInt64(formValue["ItemID"]);

    Workstation workstation = itilRepository.FindWorkstation(workstationId);
    Item item = itilRepository.FindItem(itemId);

    itilRepository.AddItem(workstation,item);
    itilRepository.Save();

    return Content("Item added successfully!");
}

我想要做的是能够使用Ajax.ActionLink将workstationId和itemId的两个参数提交给控制器,并将添加的新项目插入到网格中.我正在渲染网格:

<table>
      <tr>
        <th></th>
      <th>
        Name
      </th>
      <th>
        Service Tag
      </th>
      <th>
        Serial Number
      </th>
    </tr>

    <% foreach (var item in Model.Items) { %>

    <tr>
      <td>
        <%= Html.ActionLink("Edit","ItemEdit",new { id = item.RecordID }) %> |
        <%= Html.ActionLink("Details","ItemDetails",new { id = item.RecordID   })%>
      </td>
      <td>
        <%= Html.Encode(item.Name) %>
      </td>
      <td>
        <%= Html.Encode(item.ServiceTag) %>
      </td>
      <td>
        <%= Html.Encode(item.SerialNumber) %>
      </td>
    </tr>

    <% } %>

</table>

我的问题是当我使用ActionLink提交时,我无法弄清楚如何将参数传递给控制器​​,以及如何更新网格而不重新加载整个视图.

我真的很感激一些帮助,甚至连接一个类似的教程的链接.

谢谢!

这是工作版本,一个问题是,当控制器返回部分视图,这是所有被渲染的实际页面消失.

<% using (Ajax.BeginForm("AddItem",null,new AjaxOptions
        {
            UpdateTargetId = "ResultsGoHere",InsertionMode = InsertionMode.Replace
        },new { @id = "itemForm" } ))
{ %>

    <label for="ItemID">Item:</label>
    <%= Html.DropDownList("itemId",Model.ItemsList) %>
    <%= Html.Hidden("workstationId",Model.Workstation.RecordID) %>

    <a href="#" onclick="$('#itemForm').submit();">Submit</a>

    <div id="ResultsGoHere">
        <% Html.RenderPartial("WorkstationItems",Model.Items); %>
    </div>

<% } %>

不知道原因是什么,替换在以前工作正常,但控制器没有得到下拉值.现在控制器正在获取值,但返回的部分视图将替换整个页面.

行动方法

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddItem(string workstationId,string itemId)
{
    long lworkstationId = Convert.ToInt64(workstationId);
    long litemId = Convert.ToInt64(itemId);

    Workstation workstation = itilRepository.FindWorkstation(lworkstationId);
    Item item = itilRepository.FindItem(litemId);

    IQueryable<Item> items = itilRepository.AddItem(workstation,item);
    itilRepository.Save();

    return PartialView("WorkstationItems",items);
}

这是View的所有工作的HTML:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ITILDatabase.Models.WorkstationFormviewmodel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Workstation Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
    <link type="text/css" href="/../Content/css/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />    
    <script type="text/javascript" src="/../Content/js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="/../Content/js/jquery-ui-1.7.2.custom.min.js"></script>

    <h2>
        Workstation Details</h2>
    <fieldset>
        <legend>Fields</legend>
        <p>
            Record ID:
            <%= Html.Encode(Model.Workstation.RecordID) %>
        </p>
        <p>
            Name:
            <%= Html.Encode(Model.Workstation.Name) %>
        </p>
        <p>
            Description:
            <%= Html.Encode(Model.Workstation.Description) %>
        </p>
        <p>
            Site:
            <%= Html.Encode(Model.Workstation.Site.Name) %>
        </p>
        <p>
            Modified By:
            <%= Html.Encode(Model.Workstation.ModifiedBy) %>
        </p>
        <p>
            Modified On:
            <%= Html.Encode(String.Format("{0:g}",Model.Workstation.ModifiedOn)) %>
        </p>
        <p>
            Created By:
            <%= Html.Encode(Model.Workstation.CreatedBy) %>
        </p>
        <p>
            Created On:
            <%= Html.Encode(String.Format("{0:g}",Model.Workstation.CreatedOn)) %>
        </p>
    </fieldset>
    <fieldset>
        <legend>People</legend>
        <% Html.RenderPartial("WorkstationPeople",Model.People); %>
    </fieldset>
    <fieldset>
        <legend>Positions</legend>
        <% Html.RenderPartial("WorkstationPositions",Model.Positions); %>
    </fieldset>
    <fieldset>
        <legend>Items</legend>

            <% using (Ajax.BeginForm("AddItem","Home",new AjaxOptions
                    {
                        UpdateTargetId = "ResultsGoHere",InsertionMode = InsertionMode.Replace
                    },new { @id = "itemForm" } ))
            { %>

                <label for="ItemID">Item:</label>
                <%= Html.DropDownList("itemId",Model.ItemsList) %>
                <%= Html.Hidden("workstationId",Model.Workstation.RecordID) %>

                <a href="#" onclick="$('#itemForm').submit();">Submit</a>

                <div id="ResultsGoHere">
                    <% Html.RenderPartial("WorkstationItems",Model.Items); %>
                </div>

            <% } %>
    </fieldset>
    <br />
    <p>
        <%=Html.ActionLink("Edit","WorkstationEdit",new { id = Model.Workstation.RecordID }) %>
        |
        <%=Html.ActionLink("Back to List","Index") %>
    </p>
</asp:Content>

解决方法

您希望从AJAX电话中获得什么结果?

您可以使用AjaxHelper对象的帮助方法而不是HtmlHelper来呈现链接.例如,要使用AJAX HttpPOST调用获取内容,并将其插入< div>将ID设置为ResultsGoHere,您将渲染以下链接

<%= Ajax.ActionLink("Edit",new {
                             itemId = item.RecordId,workstationId = myWorkStationId
                         },new AjaxOptions {
                             HttpMethod="POST",UpdateTargetId="ResultsGoHere",InsertionMode = InsertionMode.Replace 
                         }) %>

在您的AcionMethod中,您可以简单地测试Request.IsAjaxRequest()来决定要返回的内容

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ItemEdit(string itemId,string workstationId) {
    // edit the item and get it back

    if (Request.IsAjaxRequest()) {
        return PartialView("SingleItem",item);
    }
    return RedirectToAction("ItemEdit",new { itemId = item.RecordId,workstationId = workstationId });
}

// fallback for get requests
public ActionResult ItemEdit(string itemId,string workstationId)
{
    // do stuff and return view
}

猜你在找的asp.Net相关文章