当用户点击表单上的提交按钮时,我想在表单上返回成功/失败消息,告诉我演示
答案 0 :(得分:2)
您可以使用视图模型:
public class MyViewModel
{
public string Message { get; set; }
}
然后有一个控制器:
public class HomeController: Controller
{
public ActionResult Index()
{
var model = new MyViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(FormCollection fc)
{
var model = new MyViewModel
{
Message = "some message"
};
return View(model);
}
}
和观点:
@model MyViewModel
@if (!string.IsNullOrEmpty(Model.Message))
{
<div>@Model.Message</div>
}
@using (Html.BeginForm())
{
<button type="submit">OK</button>
}