2012-04-13 71 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