【问题标题】:ASP.Net MVC Postback from View to Controller shows null values从视图到控制器的 ASP.Net MVC 回发显示空值
【发布时间】:2013-06-15 16:59:33
【问题描述】:

我在将 ViewModel 回发到控制器时遇到问题,但 ViewModel 没有正确地从视图映射到控制器。

TopicIdContent 应该包含值,但是,在回发时,它们不会:

VS 调试:

视图模型:

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

查看:

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

}

生成的 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>

从生成的 HTML 中可以看出,TopicId 肯定有一个值:value="7"

任何人都可以看到表单帖子和控制器之间的问题所在吗?控制器需要ReplyViewModel?

谢谢,

标记

【问题讨论】:

  • 能否展示public ActionResult List()的HTTPGET方法?

标签: c# asp.net-mvc asp.net-mvc-3 viewmodel


【解决方案1】:

您的输入字段名称以ReplyViewModel 为前缀(因为model =&gt; model.ReplyViewModel.* lambda),因此您需要将此信息指示给模型绑定器:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult List([Bind(Prefix = "ReplyViewModel")] ReplyViewModel 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
    ...
}

【讨论】:

  • 我不认为这是一个 hack。
【解决方案2】:

问题在于您的视图被输入到PostListAndReplyVM - 所以它会创建诸如ReplyViewModel.Content 这样的名称 - 但是,因为您的控制器操作需要ReplyViewModel,所以这些字段无法绑定(即不是ReplyViewModel.ReplyViewModel.Content)。

更改您的控制器操作:

public ActionResult List(PostListAndReplyVM reply)

或者 - 如果这是您的全部视图 - 只需将其键入 ReplyViewModel(并相应地更新您的 HtmlHelper 表达式)。

【讨论】:

    【解决方案3】:

    它为空,因为您将它绑定到另一个模型

    在视图中

    @model centerforum.Models.PostListAndReplyVM

    在行动ReplyViewModel

    尝试像绑定一样

    public ActionResult SomeAction(PostListAndReplyVM model)
    
        {
        }
    

    【讨论】:

    • 嗨 - PostListAndReplyVM 包含两个视图模型(我已经删除了表单外部的 HTML,用于 PostViewModel)——在表单中,我通过以下方式引用它:=> model.ReplyViewModel.TopicId - 谢谢.
    猜你喜欢
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    相关资源
    最近更新 更多