2012-10-03 38 views
0

在使用ASP.NET MVC 3的Web應用程序中,我從控制器傳遞一個模型,並將初始化屬性作爲參數傳遞給局部視圖。部分視圖返回帶有空字段的模型

該視圖顯示一個帶有單個文本框的對話框,並在啓動控制器中提交動作時被觸發(該動作與參數具有相同的模型類型)。
問題是,在這一點上,只有相對於文本框字段的屬性有一個值,即用戶插入的值,而所有其他值爲空,即使在視圖中它們具有適當的值。

一旦提交按鈕被點擊,我怎樣才能將視圖中的屬性保存到控制器?

編輯(添加的代碼):

//---------- This method in the controller call the Partial View and pass the model -------- 
[HttpPost] 
public PartialViewResult GetAddCustomFormerClubDialog() 
    { 
     var order = GetCurrentOrder(); 
     //Order has here all properties initialized 

     var dialogModel = new dialogModel<Order> { Entity = order, ControllerAddEntityActionName = "SelectOrder"}; 

     return PartialView("Dialogs/AddOrder", dialogModel); 
    } 



//----------------- Here the Partial View ----------------------------------- 
@model FifaTMS.TMS.Presentation.Model.Wizard.WizardDialogModel<Club> 

<div> 
@using (Ajax.BeginForm(Model.ControllerAddEntityActionName, "Orders", new AjaxOptions { HttpMethod = "POST"})) 
{ 
    @Html.LabelFor(a => a.Entity.Name) 
    @Html.TextBoxFor(a => a.Entity.Name, new { @class = "isrequired", style="width: 250px;" }) 
} 
</div> 



//-------- Here the method from the view (in the same controller as the first code portion) ----- 
[HttpPost] 
public JsonResult SelectOrder(dialogModel<Order> OrderModel) 
    { 
     var order= OrderModel.Entity; 
     // But order has only the property Name set (in the view) 

    ... 
    } 
+2

請張貼您的標記。我有一種感覺,你沒有將數據綁定到你的模型,因爲這些數據不在頁面上。 – Gromer

回答

1

我能夠通過添加隱藏字段爲每個需要的屬性,如簡單地解決這個問題:

@Html.HiddenFor(p => p.Entity.OrderId, new { id = "OrderId" }) 

這是因爲從PartialView Model的新實例被創建併發送到控制器。因此,只有表單中設置的屬性被採用(在我的情況下,唯一的字段是與PartialView中的TextBox相關的OrderName)。