当我发回到控制器时,模型的所有值都为空

我将模型切回了一个领域:


//模型


public class LetterViewModel

{

    public string LetterText;

}

//控制器


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控制器被激发时,该字段为空。


翻翻过去那场雪
浏览 484回答 3
3回答

largeQ

您需要在参数之前添加[FromBody]到操作[HttpPost]public ActionResult Index([FromBody]LetterViewModel model){&nbsp;&nbsp; &nbsp; //model.LetterText == null&nbsp; &nbsp; return View(model);}
打开App,查看更多内容
随时随地看视频慕课网APP