使用编辑时让我的表单自动填充
getting my form to autofill when editing using
对 mvc 很陌生。我的问题是当我有一条记录要编辑时,我的视图不会自动填写表格。具体来说,我正在尝试在拍卖网站上出价。我希望其他一切保持不变,只更新出价。如果我的表单没有自动填充,那么一切都将为空。任何帮助将不胜感激。
this is from the CarAuctionController
// GET: CarAuction/Edit/5
public ActionResult Edit(int id= 3)
{
CarList carList = db.CarLists.Find(id);
return View();
}
// POST: CarAuction/Edit/5
[HttpPost]
public ActionResult Edit(CarList carlist)
{
try
{
// TODO: Add update logic here
if (ModelState.IsValid)
{
db.Entry(carlist).State=EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View("Index");
}
catch
{
return View();
}
}
this is from the view Edit.cshtml
@model ClassicCarAuction.CarList
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>CarList</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.ModelYear, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ModelYear, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ModelYear, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Make, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Make, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Make, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Model, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Model, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Model, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CarImage, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CarImage, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CarImage, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DatePosted, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DatePosted, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DatePosted, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AuctionEndDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AuctionEndDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AuctionEndDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ReserveBid, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ReserveBid, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReserveBid, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PosterUserID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PosterUserID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PosterUserID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ViewingLocation, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ViewingLocation, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ViewingLocation, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HighestBid, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.HighestBid, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.HighestBid, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HighBidUserId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.HighBidUserId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.HighBidUserId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Status, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
问题是你返回了"view",但是你没有提供引擎可以将它们绑定在一起的"model",因此默认为空。
return View("Index");
应该是
return View("Index", carlist);
您没有将检索到的 carlist 模型传递给视图。因此视图无法访问模型并且无法填写表单。
// GET: CarAuction/Edit/5
public ActionResult Edit(int id= 3)
{
CarList carList = db.CarLists.Find(id);
// Change this:
return View(carList);
}
对 mvc 很陌生。我的问题是当我有一条记录要编辑时,我的视图不会自动填写表格。具体来说,我正在尝试在拍卖网站上出价。我希望其他一切保持不变,只更新出价。如果我的表单没有自动填充,那么一切都将为空。任何帮助将不胜感激。
this is from the CarAuctionController
// GET: CarAuction/Edit/5
public ActionResult Edit(int id= 3)
{
CarList carList = db.CarLists.Find(id);
return View();
}
// POST: CarAuction/Edit/5
[HttpPost]
public ActionResult Edit(CarList carlist)
{
try
{
// TODO: Add update logic here
if (ModelState.IsValid)
{
db.Entry(carlist).State=EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View("Index");
}
catch
{
return View();
}
}
this is from the view Edit.cshtml
@model ClassicCarAuction.CarList
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>CarList</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.ModelYear, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ModelYear, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ModelYear, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Make, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Make, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Make, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Model, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Model, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Model, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CarImage, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CarImage, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CarImage, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DatePosted, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DatePosted, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DatePosted, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AuctionEndDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AuctionEndDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AuctionEndDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ReserveBid, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ReserveBid, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReserveBid, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PosterUserID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PosterUserID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PosterUserID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ViewingLocation, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ViewingLocation, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ViewingLocation, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HighestBid, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.HighestBid, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.HighestBid, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HighBidUserId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.HighBidUserId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.HighBidUserId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Status, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
问题是你返回了"view",但是你没有提供引擎可以将它们绑定在一起的"model",因此默认为空。
return View("Index");
应该是
return View("Index", carlist);
您没有将检索到的 carlist 模型传递给视图。因此视图无法访问模型并且无法填写表单。
// GET: CarAuction/Edit/5
public ActionResult Edit(int id= 3)
{
CarList carList = db.CarLists.Find(id);
// Change this:
return View(carList);
}