2014-05-05 56 views
0

我有在訪問我的視圖模型和控制器的局部視圖模型數據複雜的模型和部分景觀

我的模型

public class SearchRequest : BaseRequest 
{ 
    public SearchOptions SearchBy{ get; set; } 
    // and other properties also there 
} 

[KnownType(typeof(SearchByAirport))] 
[KnownType(typeof(SearchByCity))] 
[KnownType(typeof(SearchByProductCodes))] 
[KnownType(typeof(SearchByGeocode))] 
public abstract class SearchOptions 
{ 
} 

public class SearchByProductCodes : SearchOptions 
{ 
    public List<string> Codes { get; set; } 
} 
public class SearchByGeocode : SearchOptions 
{ 
// few more properties 
} 

我查看

查看有問題SearchRequest的模型參考,並具有選擇搜索類別(即按產品代碼,地理代碼,城市等搜索)的下拉菜單和下拉菜單的變化我加載我的部分觀點

我的一個局部視圖

@model Tavisca.Catapult.External.DataContract.Common.SearchByProductCodes 

<div class="form-group"> 
    @Html.LabelFor(model => model.Codes, new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.TextBoxFor(model => model.Codes) 
    </div> 
</div> 

控制器

[HttpPost] 
public ActionResult Create(SearchRequest hotelSearchRequest) 
{ 
    return View(); 
} 

我得到SearchBy空在這裏,什麼是安排我的觀點,並得到所有的最好的方法從視圖到控制器的字段。

回答

0

模型聯編程序會嘗試使用您的視圖模型匹配表單值中的代碼,但它在那裏稱爲SearchBy,因此將失敗。

嘗試這樣做

<div class="form-group"> 
    @Html.LabelFor(model => model.Codes, new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.TextBoxFor(model => model.Codes, new {Name="SearchBy" }) 
    </div> 
</div> 

這將使模型綁定正確的字段名稱拔出。

另一種方式是從表單手動讓它在你的控制器動作是這樣的:

Request.Form["Codes"] 

就個人而言,我會選擇選項#1。