1

我想将输入模型从局部视图传递给控制器​​。我对 MVC 比较陌生,所以仍在尝试了解默认模型绑定器的工作原理。

通过 AJAX (listBox) 控制器将部分视图传回并插入到表 id=searchResults 中。

@model ViewModels.LocationViewModel
@using (Ajax.BeginForm("ProcessSearch", "SearchR", new AjaxOptions
{
    HttpMethod = "GET",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "searchResults",
}))

{ 
    <div>@Html.ListBoxFor(xxx)</div>
    <input id="Search" type="submit" value="Search" />
}

这是填充局部视图的控制器和 ViewModel

public class OrderViewModel
{
    public string Description       { get; set; }
    public string Status            { get; set; }     
}

public ActionResult ProcessSearch(SearchViewModel search)
{
     select new OrderViewModel{
                Status=f.STATUS,
                Description=f.DESCRIPTION}).ToList();
     return PartialView(model);
}

在同一个主视图中,我有这个我想要绑定到另一个视图模型的表单。我根本不明白如何从局部视图的模型中实现默认活页夹。如果我没有正确解释这一点,我深表歉意。我希望这是有道理的。

@using (Html.BeginForm("Testing", "SearchR", FormMethod.Post))
{ 
    <div>@Html.DropDownListFor(yyy)</div>
    <input id="Reshop" type="submit" value="Reshop" />
}
<table id="searchResults"></table>


public ActionResult Testing(RSOrderViewModel rOrder)
{
    return Content("hey");
}

public class RSOrderViewModel 
{
    public string yyy { get; set; }
    public IEnumerable<OrderViewModel> sovm { get; set; }
}


@model List<ViewModels.OrderViewModel>

@{ViewData.TemplateInfo.HtmlFieldPrefix = "sovm";
   }

 <table id="searchResults">
    <tr>
        <th>Order Id</th>
        <th>Order Detail</tr>

@for (int i = 0; i < Model.Count; i++)
{
    <tr>
        <td>
         @Html.DisplayFor(x => x[i].OrderId)
        </td>
        <td>
         @Html.DisplayFor(x => x[i].OrderDetail)
        </td>
    </tr>
}

</table>
4

1 回答 1

3

该表在第二种形式之外。因此,当您发布到Testing操作时,发送到控制器的所有内容都是下拉列表的值。如果要发送存储在此表中的集合,则必须使用 AJAX 或将表放入表单中:

@using (Html.BeginForm("Testing", "SearchR", FormMethod.Post))
{ 
    <div>@Html.DropDownListFor(yyy)</div>
    <table id="searchResults"></table>
    <input id="Reshop" type="submit" value="Reshop" />
}

当然,现在将表格放在表单中并不意味着它会在您提交表单时向服务器发送任何内容。您需要放置输入字段(如果您不希望它们对用户可见,则隐藏),其中将包含将被 POST 回的值。此外,这些输入字段名称必须遵循绑定到列表的标准约定

您实际上并没有展示局部视图的外观,但这里有一个示例说明它的外观以便遵守约定。例如,假设您在 OrderViewModel 中有 2 个要绑定的属性:OrderId 和 OrderDetail:

@model IEnumerable<OrderViewModel>
@{
    // We set the name prefix for input fields to "sovm"
    // because inside your RSOrderViewModel the collection 
    // property you want to bind to this table is called sovm
    // and in order to respect the convention the input names
    // must be in the following form "sovm[0].OrderId", "sovm[1].OrderId", ...
    ViewData.TemplateInfo.HtmlFieldPrefix = "sovm";
}
<thead>
    <tr>
        <th>Order Id</th>
        <th>Order detail</th>
    </tr>
</thead>
<tbody>
    @Html.EditorForModel()
</tbody>

然后你可以有一个编辑器模板,它将为模型的每个元素(~/Views/Shared/EditorTemplates/OrderViewModel.cshtml)呈现:

@model OrderViewModel
<tr>
    <td>@Html.EditorFor(x => x.OrderId)</td>
    <td>@Html.EditorFor(x => x.OrderDetail)</td>
</tr>

模板的名称是要绑定到的集合中使用的类型的名称 ( IEnumerable<OrderViewModel> sovm { get; set; }=> OrderViewModel.cshtml)。~/Views/Shared/EditorTemplates如果它可以在多个控制器之间重复使用,或者它特定于当前控制器,则它也必须放在文件~/Views/XXX/EditorTemplates夹中,其中 XXX 是当前控制器。

于 2012-04-13T05:54:25.987 回答