ASP.NET MVC 3模型绑定和表单域 [英] ASP.NET MVC 3 Model-binding and form fields

查看:100
本文介绍了ASP.NET MVC 3模型绑定和表单域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为实体 Domain.Models.BlogPost ,其中包含以下属性:

I have an entity called Domain.Models.BlogPost which contains the following properties:


  • 帖子ID

  • 标题

  • 作者

  • PostedDate

  • 正文

我也有叫实体 Domain.Models.PostComment ,其中包含以下属性:

I also have an entity called Domain.Models.PostComment which contains the following properties:


  • CommentID

  • 帖子ID

  • 作者

  • 电子邮件

  • 网站

  • 正文

博文包含许多 PostComments 。一对多的关系。

BlogPost contains many PostComments. A one to many relationship.

现在我有这样的(从博客帖子code通过HTML注释分隔评论表单)一个观点:

Now I have a view like this (separated comment form from blog post code via html comment):

@model Domain.Models.BlogPost
@using Domain.Models;
@{
    ViewBag.Title = "Post";
}
<div class="postTitle">@Model.Title</div>
<div class="subInfo">
    Posted by @Model.Author on @Model.PostedDate.ToString("D")
</div>
<div class="postBody">
    @Html.Markdown(Model.Body)
</div>
<br />
@Model.PostComments.Count Comment(s).
<div class="comments">
@foreach (PostComment postComment in Model.PostComments)
{
    Html.RenderPartial("PostComment", postComment);
}
</div>

<!-- BELOW IS THE ADD COMMENT FORM -->

<div id="addComment">
@using (Html.BeginForm("AddComment", "Blog"))
{
    <text>
    @Html.Hidden("PostID", Model.PostID)<br />
    Name: @Html.TextBox("Author")<br />
    Email: @Html.TextBox("Email")<br />
    Website: @Html.TextBox("Website")<br />
    Body: @Html.TextArea("Body")<br />
    <input type="submit" value = "Add Comment" />
    </text>
}
</div>
@Html.ActionLink("Add Comment", "AddComment")

问题是,由于评论表单使用 @ Html.TextBox(作者) @ Html.TextBox(身体) ,它们填充了从模型,其中还包含的属性数据作者机身 。关于如何解决这一问题,以便这些领域没有得到摆在他们价值观的任何建议在页面加载时?

The problem is that because the comment form uses @Html.TextBox("Author") and @Html.TextBox("Body"), they are populated with data from the model, which also contains properties Author and Body. Any suggestions on how to fix this so these fields don't get values put in them when the page loads?

我也尝试创建一个 BlogPostViewModel 和设置,作为视图模式和分配与博文属性我实际的模型:

I also tried creating a BlogPostViewModel and setting that as the model of the view and assigning the BlogPost property with my actual model:

public class BlogPostViewModel
{
    public BlogPost BlogPost { get; set; }
    public PostComment NewComment { get; set; }
}

然后我做了 @ Html.TextBoxFor(X =&GT; x.NewComment.Author)但是当张贴到这一行动方法的形式为:

Then I did @Html.TextBoxFor(x => x.NewComment.Author) but when the form posted to this action method:

public ActionResult AddComment(PostComment postComment)
{
    // ...
}

postComment 没有绑定到表单值:/

postComment did not bind to the form values :/

推荐答案

您既可以重命名 AddComment 部分的东西,不与命名的属性发生冲突的领域在模型中,或者你可以使用 Html.TextBox 文本框此重载采用

You could either rename the fields in the AddComment section to something that does not collide with the properties named in the Model, or you could override the value in the view using a different overload of Html.TextBox This overload of TextBox takes a value:

值(类型:System.Object)结果
      文本输入元素的值。如果这个值是零,则该元素的值是从的ViewDataDictionary对象检索。如果没有值存在时,该值从ModelStateDictionary对象检索

value (Type: System.Object)
The value of the text input element. If this value is null, the value of the element is retrieved from the ViewDataDictionary object. If no value exists there, the value is retrieved from the ModelStateDictionary object.

更新:既然你加入NewComment作为一个属性,解决了财产命名冲突的方式,所有你需要做的绑定PostComment而不是POST全视图模型的动作,是指导模型粘合剂,一个preFIX将被使用。这是通过使用做了 BindAttribute


UPDATE: Since you added "NewComment" as a property and resolved the property naming collision that way, all that you need to do to bind a PostComment rather than the whole view model on POST to the action, is to instruct the model binder that a prefix will be used. This is done using the BindAttribute.

public ActionResult AddComment([Bind(Prefix = "NewComment")] PostComment postComment)

这篇关于ASP.NET MVC 3模型绑定和表单域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆