回发时丢失了模型值

时间:2011-12-10 22:51:54

标签: asp.net-mvc asp.net-mvc-3 post postback

我有以下型号:

class A
{
    // ...some properties

    public B InnerField { get; set; }
}

class B
{
    public int Id { get; set; }

    // ..other properties
}

和一个模型类A的页面在页面内部我有一个局部视图绑定到表单内的B类。 Id(在局部视图中)的值正确设置为模型的Id值(不同于0),但是当我提交页面时,模型的Id值为0. Id值不会在组件或其他地方修改。

...other parts of main page

<%using (Html.BeginForm("ModifyHotel", "Hotel",
                   FormMethod.Post, new { enctype = "multipart/form-data"}))
   {%>  
     <% Html.RenderPartial("~/Views/Shared/ModifyBaseItem.ascx", 
                   new ModifyItemRequestBaseView() { ItemId = Model.Item.Id });%>
 <%}%>

...other parts of main page

部分视图

...other parts of partial view
<br/>      
    Add Photo: <%:Html.FileBoxFor(x => x.PhotoFile, null)%>            
    <br/>    
    Add Video: <%:Html.FileBoxFor(x => x.VideoFile, null)%>            
    <br/>
<input type="submit" value="Submit changes" /> 
...other parts of partial view

在发布帖子时,我该怎么做才能保持内模的价值?

谢谢,

1 个答案:

答案 0 :(得分:2)

<强>控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        A model = new A() { InnerField = new B() { Id = 5 }};
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(B model)
    {
        //on postback the model should have the value 5 here
        return View();
    }
}

查看:

@model MvcApplication11.Models.A

@using (Html.BeginForm())
{
    @Html.Partial("_IndexForm", Model.InnerField)

    <input type="submit" />
}

<强>部分:

@model MvcApplication11.Models.B

@Html.EditorFor(m => m.Id)