0
我之前遇到过编辑视图出现问题的问题。这已经从此解决了post从我的编辑视图中未反映在数据库中的更新
现在我需要从我的MVC视图进行更新。更新似乎没有提交到数据库,因为更改不会反映在我进行更新时。下面是我更新的ActionResult:
[HttpGet]
public ActionResult Update()
{
EditGameVM model = new EditGameVM();
return View(model);
}
[HttpPost]
public ActionResult Update(EditGameVM model)
{
try {
game objGame = new game
{
gameID = model.gameID,
gameName = model.gameName,
description = model.description,
gameRule = model.gameRule,
gamePicture = model.gamePicture.ToString()
};
objBs.gameBs.Update(objGame);
TempData["Msg"] = "Created Successfully!";
return RedirectToAction("Edit");
}
catch (DbEntityValidationException dbEx)
{
...
}
}
,这里是相应的视图:
@model TeamBuildingCompetition.ViewModels.EditGameVM
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout_new.cshtml";
}
<script>
tinymce.init({ selector: '#description' });
tinymce.init({ selector: '#gameRule' });
</script>
@using (Html.BeginForm("Update", "Game", FormMethod.Post))
{
@Html.AntiForgeryToken()
<section id="middle">
<div class="container">
<div class="form-horizontal">
<div class="center"><h1>Edit Games </h1></div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.gameID)
<div class="form-group">
@Html.LabelFor(model => model.gameName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-8">
@Html.EditorFor(model => model.gameName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.gameName, "", 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-8">
@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.gameRule, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-8">
@Html.EditorFor(model => model.gameRule, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.gameRule, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.gamePicture, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-8">
@Html.EditorFor(model => model.gamePicture, new { htmlAttributes = new { @class = "form-control" }})
</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>
</section>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
什么是错误? –
你有什么异常吗?并把你的gameBs.Update(objGame);方法代码。 –
我没有看到你在dbcontext上调用'SaveChanges()' – user1666620