简体   繁体   English

从视图到控制器的ASP.Net MVC回发显示空值

[英]ASP.Net MVC Postback from View to Controller shows null values

I've a problem with ViewModel posting back to a controller, but the ViewModel not being mapped correctly from the View to the Controller. 我在将ViewModel发布回控制器时遇到问题,但是ViewModel没有从View正确映射到Controller。

TopicId and Content should contain values, however, when posted back, they do not: TopicIdContent应该包含值,但是在回发时,它们不能:

VS Debug: VS调试: ss

ViewModels: ViewModels:

  public class PostViewModel
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Author { get; set; }
    public DateTime DateOfTopic { get; set; }
}

public class ReplyViewModel
{
    public int TopicId { get; set; }
    public string Content { get; set; }

}

public class PostListAndReplyVM
{
    public List<PostViewModel> PostViewModel { get; set; }
    public ReplyViewModel ReplyViewModel { get; set; }
}

View: 视图:

@model centreforum.Models.PostListAndReplyVM

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Post</legend>

         @Html.HiddenFor(model => model.ReplyViewModel.TopicId)

    <div class="editor-label">
        @Html.LabelFor(model => model.ReplyViewModel.Content)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ReplyViewModel.Content)
        @Html.ValidationMessageFor(model => model.ReplyViewModel.Content)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

} }

Generated HTML: 生成的HTML:

<form action="/Post/List/7/" method="post"><input name="__RequestVerificationToken" type="hidden" value="xxxxxxxxxxxxx" />    <fieldset>
    <legend>Post</legend>

         <input data-val="true" data-val-number="The field TopicId must be a number." data-val-required="The TopicId field is required." id="ReplyViewModel_TopicId" name="ReplyViewModel.TopicId" type="hidden" value="7" />

    <div class="editor-label">
        <label for="ReplyViewModel_Content">Content</label>
    </div>
    <div class="editor-field">
        <input class="text-box single-line" id="ReplyViewModel_Content" name="ReplyViewModel.Content" type="text" value="" />
        <span class="field-validation-valid" data-valmsg-for="ReplyViewModel.Content" data-valmsg-replace="true"></span>
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
</form>

As you can see from the generated HTML, the TopicId definitely has a value: value="7" 从生成的HTML中可以看到,TopicId肯定有一个值: value="7"

Can anyone see where the problem is between the form post, and the controller, which is expecting the ReplyViewModel? 谁能看到问题出在表单发布和期望ReplyViewModel的控制器之间?

Thank you, 谢谢,

Mark 标记

Your input field names are prefixed with ReplyViewModel (because of the model => model.ReplyViewModel.* lambda), so you need to indicate this information to the model binder: 输入字段名称以ReplyViewModel为前缀(由于model => model.ReplyViewModel.* lambda),因此您需要向模型绑定程序指示此信息:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult List([Bind(Prefix = "ReplyViewModel")] ReplyViewModel model)
{
    ...
}

Alternatively have your List action take the PostListAndReplyVM model: 或者,让您的List操作采用PostListAndReplyVM模型:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult List(PostListAndReplyVM model)
{
    // obviously only model.ReplyViewModel will be bound here because
    // those are the only input fields in your form
    ...
}

The problem is the fact that your view is typed to PostListAndReplyVM - so it creates names such as ReplyViewModel.Content - but, because your controller action expects a ReplyViewModel , these fields can't be bound (ie there is no such thing as ReplyViewModel.ReplyViewModel.Content ). 问题在于您的视图键入了PostListAndReplyVM因此它创建了诸如ReplyViewModel.Content名称-但是,因为您的控制器操作需要一个ReplyViewModel ,所以无法绑定这些字段(即,没有诸如ReplyViewModel.ReplyViewModel.Content类的东西ReplyViewModel.ReplyViewModel.Content )。

Change your controller action: 更改您的控制器操作:

public ActionResult List(PostListAndReplyVM reply)

Alternatively - if that's your whole view - just type it to ReplyViewModel instead (and update your HtmlHelper expressions accordingly). 或者,如果这是您的整个视图,则只需将其键入ReplyViewModel (并相应地更新HtmlHelper表达式)。

Its null because you bound it to another model 它为null,因为您将其绑定到另一个模型

In view 视野中

@model centreforum.Models.PostListAndReplyVM @model centreforum.Models.PostListAndReplyVM

In Action ReplyViewModel 在行动中ReplyViewModel

try to bind like 尝试像

public ActionResult SomeAction(PostListAndReplyVM model)

    {
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM