我创建了一个简单的MVC应用程序。它在文件夹home中有一个名为Index的视图,在视图中我有一些输入框,我希望保存为对象的输入并保存在数据库中。我已经从我创建的控制器创建视图。我的问题是当我点击提交按钮似乎什么都没发生?
这是我在视图中的代码。
@model FlexMVC.FlexBookingDb.Resevation
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Resevation</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ResevationDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ResevationDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ResevationDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ResevationTime, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ResevationTime, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ResevationTime, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Car, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Car, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Car, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Customer, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Customer, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Customer, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartLocation, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StartLocation, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StartLocation, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndLocation, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EndLocation, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EndLocation, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
这是我的控制器代码。
namespace FlexMVC.Controllers
{
public class ResevationController : Controller
{
// GET: Resevation
public ActionResult Index()
{
return View();
}
// POST: Resevation
[HttpPost]
public ActionResult Create(Resevation resevation)
{
ResevationModels model = new ResevationModels();
resevation.Car = 1;
resevation.Customer = 1;
model.CreateResevation(resevation);
return View(model);
}
}
}
任何人都可以向我解释我做错了什么吗?并且请说我是否应该发布模型代码。
编辑:
这是CreateResevation方法的代码。
public void CreateResevation(Resevation resevation)
{
resevation.Car = FindAvaliableCar(resevation.ResevationDate, resevation.ResevationTime);
_db.Resevations.InsertOnSubmit(resevation);
_db.SubmitChanges();
}
答案 0 :(得分:2)
你有两个问题。
您可能提交了错误的操作。您可能希望提交创建,然后提交索引。这可以通过更改为@using (Html.BeginForm("Create", "ControllerNameHere"))
AntiForgeryToken 需要针对建议的操作设置[ValidateAntiForgeryToken()]
验证属性。
将其更改为
[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult Create(Resevation resevation)