我正在尝试使用编辑视图模型。在[httpget]上,它没有问题,但在[httppost]上,它不会保存/更新到数据库。它将我发送到我的http错误404.0 - 未找到。
这是我的代码:
在我的节日控制器中编辑动作:
[HttpGet]
public ActionResult Edit2(int? id)
{
//Festival fest = db.Festivals.Include("County").Include("FestivalType").Include("Towns").Where(x => x.FestivalId == id).Single();
Festival fest = db.Festivals.Where(x => x.FestivalId == id).FirstOrDefault();
FestivalVM festival = new FestivalVM { SelectedCounty = fest.FestivalCounty.ID, endDate = fest.EndDate, FestivalName = fest.FestivalName, SelectedFestivalType = fest.FType.ID, startDate = fest.StartDate, SelectedTown = fest.FestivalTown.ID };
if (fest != null)
{
festival.County = db.Counties.ToDictionary(p => p.ID, q => q.Name);
festival.FestivalType = db.FestivalTypes.ToDictionary(p => p.ID, q => q.FType);
festival.FestivalType.Add(-1, "----- Add New Festival Type -----");
festival.Towns = db.Towns.ToDictionary(p => p.ID, q => q.Name);
festival.startDate = (from f in db.Festivals where fest.FestivalId == id select f.StartDate).FirstOrDefault();
festival.endDate = (from f in db.Festivals where fest.FestivalId == id select f.EndDate).FirstOrDefault();
return View(festival);
}
return HttpNotFound();
}
[HttpPost]
public ActionResult Edit2(FestivalVM model)
{
if (ModelState.IsValid != true)
{
if (model.SelectedFestivalType != -1)
{
Festival f = new Festival();
//db.save stuff from create.
f.EndDate = model.endDate.Date;
f.FestivalCounty = db.Counties.Where(p => p.ID == model.SelectedCounty).Single();
f.FestivalName = model.FestivalName;
f.FType = db.FestivalTypes.Where(p => p.ID == model.SelectedFestivalType).Single();
f.StartDate = model.startDate.Date;
f.FestivalTown = db.Towns.Where(p => p.ID == model.SelectedTown).Single();
f.UserID = WebSecurity.CurrentUserId;
//db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Details", "Festival", new { id = f.FestivalId });
//String test = "test3";
}
ModelState.AddModelError("", "No Festival Type Picked");
}
model.County = db.Counties.ToDictionary(p => p.ID, q => q.Name);
model.FestivalType = db.FestivalTypes.ToDictionary(p => p.ID, q => q.FType);
model.FestivalType.Add(-1, "----- Add New -----");
model.Towns = db.Towns.ToDictionary(p => p.ID, q => q.Name);
model.startDate = DateTime.Now;
model.endDate = DateTime.Now;
return View(model);
}
修改视图
@model MyFestival.Models.FestivalVM
@{
ViewBag.Title = "Edit Your Festival";
Layout = "~/Views/Shared/Festival.cshtml";
}
<h2>Edit Your Festival</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.FestivalName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.FestivalName, new{@class = "form-control", @style="width:250px" })
@Html.ValidationMessageFor(model => model.FestivalName, null, new {@style="color:red;"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.startDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.startDate, new { @class = "form-control", @style = "width:250px" })
<input class="form-control datepicker" style="width:250px" name="startDate" placeholder="Please pick date..."/>
@Html.ValidationMessageFor(model => model.startDate, null, new { @style = "color:red;" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.endDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.endDate, new { @class = "form-control", @style = "width:250px" })
<!--<input class="form-control datepicker" style="width:250px" name="endDate" placeholder="Please pick date..."/>-->
@Html.ValidationMessageFor(model => model.endDate, null, new { @style = "color:red;" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Towns, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(p=> p.SelectedTown ,Model.Towns.Select(p=> new SelectListItem(){Text=p.Value.ToString(), Value=p.Key.ToString(),Selected=false}), new{@class = "form-control", @style="width:250px" })
@Html.ValidationMessageFor(model => model.Towns, null, new {@style="color:red;"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.County, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(p=> p.SelectedCounty, Model.County.Select(p=> new SelectListItem(){Text=p.Value.ToString(),Value=p.Key.ToString(), Selected=false}), new{@class = "form-control", @style="width:250px" })
@Html.ValidationMessageFor(model => model.County, null, new {@style="color:red;"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FestivalType, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(p=> p.SelectedFestivalType, Model.FestivalType.Select(p=> new SelectListItem(){Text=p.Value.ToString(),Value=p.Key.ToString(), Selected=false}), new{@class = "form-control", @style="width:250px"})
@Html.ValidationMessageFor(model => model.FestivalType, null, new {@style="color:red;"})
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-info" />
@Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-danger" })
</div>
</div>
<br />
</div>
}
@Html.Partial("CreateFestivalType", new MyFestival.Models.FestivalTypeVM())
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(document).ready(function () { // will trigger when the document is ready
$('.datepicker').datepicker({
format: 'dd-mm-yyyy',
currentText: 'Now'
}); //Initialise any date pickers
});
</script>
<script>
$(document).ready(function () {
$('#SelectedFestivalType').change(function () {
if ($(this).find(":selected").val() == -1) {
$('#myModal').modal('show');
$('.focus :input:first').focus();
}
});
});
</script>
<script type="text/javascript">
function ajaxResponse(data) {
alert("This Worked and the Data ID is: " + data.FestivalTypeID);
var newOption = "<option value='" + data.FestivalTypeID + "'>" + data.Name + "</option>";
$('#SelectedFestivalType').append(newOption);
$('#myModal').modal('hide');
$("#SelectedFestivalType option[value='" + data.FestivalTypeID + "']").attr("selected", "selected");
};
</script>
}
答案 0 :(得分:0)
要保存更改,请在调用db.SaveChanges()之前添加此行:
db.Festival.Add( f );
您没有添加到上下文中。