c# - 当我回发到 Controller 时,模型的所有值均为空

标签 c# asp.net-mvc

我已将模型切回到一个字段:

//型号

public class LetterViewModel
{
    public string LetterText;
}

// Controller

public ActionResult Index()
{
    var model = new LetterViewModel();
    model.LetterText = "Anything";

    return View(model);
}

[HttpPost]
public ActionResult Index(LetterViewModel model)
{ 
    //model.LetterText == null
    return View(model);
}

//查看

@model Test.Models.LetterViewModel
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "Create a Letter";
}
@using (Html.BeginForm())
{
    <div id="Bottom">
        @Html.TextAreaFor(m => m.LetterText)
        <input type="submit" value="Ok" class="btn btn-default" />
    </div>
}

当我检查开发工具中的“网络”选项卡时,它显示输入的值已包含在请求中。但是,当 HttpPost Controller 被触发时,该字段为空。

最佳答案

DefaultModelBinder 不设置字段的值,仅设置属性。您需要更改模型以包含属性

public class LetterViewModel
{
    public string LetterText { get; set; } // add getter/setter
}

关于c# - 当我回发到 Controller 时,模型的所有值均为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46929899/

相关文章:

c# - c#动态添加对象属性

c# - C# 中的 MS Chart 中的#VALX 值不是 "M/d H:mm"格式

asp.net-mvc - 网站重建和保留 Google Analytics

c# - Pin 支付 API - "Authentication failed because remote party has closed the transport stream"

javascript - 在服务器端更新字段后防止触发 OnChange 函数

c# - 使用 Entity Framework 6.1 CodeFirst 的 SQL Server 2014 内存优化表

c# - Observable TcpListener 在单连接后终止

c# - 如何使用对象上下文在 Entity Framework 中使用批量插入?

asp.net-mvc - 调用具有参数的 lambda 表达式的方法时,我可以使用命名参数吗?

asp.net-mvc - 上传图片 - 安全