简体   繁体   中英

Pass a Model from a Controller to a View MVC 5

I am new to MVC and created a view with Entity. I have been looking for the last day how to pass a model from controller to a view. I see that when they gonna send a model back to the view they do it with return view(model); , but every time I try returning a model all the textbox are empty and nothing happens. I am trying to fill 40 Textbox that's the reason I am trying to use a model if there is a better way to do this.

HTML:

@model SiCom.Data.TransComm 
@{ ViewBag.Title = "ManualAdvancements"; }

<h2>ManualAdvancements</h2>

@using (Html.BeginForm()) { @Html.AntiForgeryToken()
  <div class="form-group">
    @Html.LabelFor(model => model.Master_Company, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
      @Html.EditorFor(model => model.Master_Company, new { htmlAttributes = new { @class = "form-control" } })
      @Html.ValidationMessageFor(model => model.Master_Company, "", new { @class = "text-danger" })
    </div>
  </div>
}

Controller:

[HttpPost]
public ActionResult ManualAdvancements(TransComm model, string submit, FormCollection form)
{
    string Paramater = form["Paramater"].ToString();
    int Parameter2= int.Parse(form["Parameter2"].ToString());
    string Paramater3= form["Paramater3"].ToString();
    model = Data.SiComData.GetSingleTransCom(Paramater, Parameter2, Paramater3);
    return View(model);
}

After all, the view is still Empty. Any Help will be apricieted.

在将模型返回到视图之前,您应该尝试使用ModelState.Clear()

I cant see anything wrong in your code snip. Only thing I would say is -

  1. Ensure that your model in the controller file(and in the method) in this line is not NULL

model = Data.SiComData.GetSingleTransCom(Paramater, Parameter2, Paramater3); return View(model);

  1. Other thing is in the View fist line should have @model Fully.Qualified.Namespace.ModelName

Hope it helps you....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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