gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:03:05 24 4
gpt4 key购买 nike

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

//型号

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com