2012-04-13 72 views
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" /> 
} 

这里是控制器和视图模型用于填充局部视图

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> 

回答

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" /> 
} 

当然现在把表的形式内部并不意味着它会在提交表单时将任何内容发送到服务器。您需要将输入字段(隐藏,如果您不希望它们对用户可见)将包含将被回发的值。此外,这些输入字段名称必须遵循standard convention以绑定到列表。

实际上并未显示局部视图的外观如何,但下面是一个示例,说明如何看待该惯例。例如,让我们假设你有你的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是当前控制器。

+0

嗨达林,我在上面加了你提到的改变的部分视图。基于我有限的理解,我添加了一个for循环来添加一个前缀计数器。它没有工作,这是很好的,因为它给了我一些事情来做这个周末...... arg。我没有使用编辑器模板......这很重要吗?我想我会用一张桌子。再次感谢您的帮助 – Mustang31 2012-04-13 19:36:32

+0

@ Mustang31,在您展示的示例中,“

”标签仍然不在表格中。这就是为什么它不起作用。除此之外,使用for循环的方法应该使用与编辑器模板相同的标记。这只是它更麻烦。就我个人而言,我宁愿让框架为我完成这项工作,而仅仅依靠这些惯例。 – 2012-04-13 20:14:01

+0

达林,对不起,我没有更新上述帖子的部分,但在我的代码中,我没有将表格添加到表单中。 – Mustang31 2012-04-13 20:55:06