簡體   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