回发时模型值丢失

发布于 2024-12-20 11:48:55 字数 1156 浏览 3 评论 0原文

我有以下模型:

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

如何在发帖时保留内部模型的值?

谢谢,

I have the following models:

class A
{
    // ...some properties

    public B InnerField { get; set; }
}

and

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

    // ..other properties
}

and a page that has a model Class A and inside the page I have a partial view bound to Class B inside a form.
The value of the Id (in the partial view) is set correctly to the model's Id value (different from 0) but when I submit the page the model has the Id value 0. The Id value is not modified in the component or elsewhere.

Page

...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

Partial View

...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

What can I do to keep the value of the inner model when the post is made?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[旋木] 2024-12-27 11:48:55

控制器:

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)

Controller:

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();
    }
}

View:

@model MvcApplication11.Models.A

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

    <input type="submit" />
}

Partial:

@model MvcApplication11.Models.B

@Html.EditorFor(m => m.Id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文