繁体   English   中英

MVC传递模型来查看

[英]MVC pass model to view

我的Index.cshtml

@model ReportGenerator.WebUI.Models.ReportViewModel
@Html.TextBoxFor(m => m.report.FileName, new { @class = "form-control", id = "FileName" })

我的控制器

public ActionResult Index(ReportViewModel model)
{
    ...some stuff
    model.report = new Report();
    model.report.FileName = "INDEX";
    return View(model);
}

public ActionResult fillFields(ReportViewModel _model)
{
    ...some stuff
    _model.report = new Report();
    _model.report.FileName = "FILL";
    return View("Index", _model);
}

当我运行我的应用程序时, TextBox Text属性设置为“INDEX”。 此外,当我单击调用fillFields控制器操作的按钮时, TextBox仍然显示“INDEX”,它不会更改为“FILL”。

我究竟做错了什么? 为什么不想工作?

@StephenMuecke在上面的评论中正确回答了这个问题。

你没有做错什么。 fillFields()方法有一个参数ReportViewModel,因此在初始化方法时将其值添加到ModelState。 返回视图时, TextBoxFor()方法使用ModelState的值(而不是model属性)来设置文本框的值。 这里解释其原因。 正确的方法是遵循PRG模式 -

暂无
暂无

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

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