如何将此模型值从视图传递给控制器方法?
查看:
@using (Html.BeginForm("SearchMethod", "Home", new { area = "Timetables" }))
{
@Html.TextBoxFor(modelItem => modelItem.Sessionstateadminroot.CourseSearchPara)
<button type="submit" >Search</button>
}
控制器方法:
[Route("SearchMethod/{searchpara=Test}")]
public ActionResult SearchMethod(string searchpara)
{
答案 0 :(得分:0)
这是一个简单的演示,传递包含另一个
的模型 public class ParentModel
{
public ChildModel ChildModel { get; set; }
public string Name { get; set; }
}
public class ChildModel
{
public string Name { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
ParentModel model = new ParentModel();
model.Name = "Parent model";
model.ChildModel = new ChildModel();
model.ChildModel.Name = "Child Name";
return View(model);
}
public ActionResult SearchMethod (ParentModel model)
{
var name = model.Name;
var childName = model.ChildModel.Name;
return View(model);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
}
观点:
@model MVCModelDemo.Models.ParentModel //Note the model passed to the view here
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm("SearchMethod", "Home", new { area = "" }))
{
@Html.TextBoxFor(modelItem => modelItem.Name)
@Html.TextBoxFor(modelItem => modelItem.ChildModel.Name)
<button type="submit">Search</button>
}
我希望这可以帮助您理解模型,控制器和视图之间的关系