我有一种情况,我有一个表单,希望管理员修改他们的考试/调查详细信息,我正在尝试使用以下代码更新数据库表。
但是该代码不会将我对“ if”部分的更改保存在控制器中,并且不会引发任何错误,只会将我重定向到下一个页面,即“ EditExam2”。
我正在尝试更新“ InformationSheetText”和“ InformationConsentForm”字段。
我知道在数据库中添加新行时,查询就像控制器中代码的“其他”部分一样工作。
我的观点
@model
AppXamApplication.Models
InformationSheetViewModel
@{
ViewBag.Title = "InformationSheet";
}
<!DOCTYPE html>
<html>
<body>
<h2>InformationSheet</h2>
<h3>Survey ID: @ViewBag.CurrentExamID</h3>
@using (Html.BeginForm("InformationSheet", "ExamAdmin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<h4>Create Information and Consent Sheet.</h4>
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.ImageURL, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
<input type="file" name="ImageFile" />
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.InformationSheetText, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.EditorFor(m => m.InformationSheetText, new { @class = "form-control", @rows = 4, @style = "resize: none;" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
@Html.CheckBoxFor(m => m.Check_InformationSheet, new { @disabled = "disabled", @checked = true })
@Html.LabelFor(m => m.Check_InformationSheet, new { })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.InformationConsentForm, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.EditorFor(m => m.InformationConsentForm, new { @class = "form-control", @rows = 4, @style = "resize: none;" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
@Html.CheckBoxFor(m => m.Check_InformationConsentForm1, new { @disabled = "disabled", @checked = true })
@Html.LabelFor(m => m.Check_InformationConsentForm1, new { })
</div>
<div class="col-md-10">
@Html.CheckBoxFor(m => m.Check_InformationConsentForm2, new { @disabled = "disabled", @checked = true })
@Html.LabelFor(m => m.Check_InformationConsentForm2, new { })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Create Exam" />
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我的模特
public class InformationSheetViewModel
{
public string ExamID { get; set; }
[Display(Name = "Choose Image To Display")]
public string ImageURL { get; set; }
[Display(Name = "Enter your Information Sheet")]
public string InformationSheetText { get; set; }
[Display(Name = "Enter your Consent Form")]
public string InformationConsentForm { get; set; }
public HttpPostedFileBase ImageFile { get; set; }
[Display(Name = "I had read and understood the information sheet")]
public bool Check_InformationSheet { get; set; }
[Display(Name = "I consent and agree to the information consent form")]
public bool Check_InformationConsentForm1 { get; set; }
[Display(Name = "I have read, agree and consent to the information and conditions")]
public bool Check_InformationConsentForm2 { get; set; }
}
我的控制器
[HttpGet]
public ActionResult InformationSheet(string id)
{
if (ModelState.IsValid)
{
ViewBag.CurrentExamID = id;
using (var ctx = new AppXamApplicationEntities())
{
var query = ctx.InformationConsentAndSheets.Where(x => x.ExamID.Equals(id)).Select(x => new InformationSheetViewModel()
{
ExamID = id,
InformationSheetText = x.InformationSheetText,
InformationConsentForm = x.InformationSheetText
}).FirstOrDefault();
return View(query);
}
}
return View();
}
[HttpPost]
[Authorize(Roles = "ExamAdmin")]
[ValidateAntiForgeryToken]
public ActionResult InformationSheet(string id, InformationSheetViewModel model)
{
using (var ctx = new AppXamApplicationEntities())
{
InformationConsentAndSheet query = ctx.InformationConsentAndSheets.Where(x => x.ExamID.Equals(id)).FirstOrDefault();
if (query != null)
{
//To insert picture into database as well as folder
string fileName = Path.GetFileNameWithoutExtension(model.ImageFile.FileName);
string extension = Path.GetExtension(model.ImageFile.FileName);
fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
model.ImageURL = "~/Image/" + fileName;
fileName = Path.Combine(Server.MapPath("~/Image/"), fileName);
model.ImageFile.SaveAs(fileName);
query = new InformationConsentAndSheet()
{
ExamID = id,
ImageURL = model.ImageURL,
InformationSheetText = model.InformationSheetText,
InformationConsentForm = model.InformationConsentForm
};
ctx.SaveChanges();
}
else
{
//To insert picture into database as well as folder
string fileName = Path.GetFileNameWithoutExtension(model.ImageFile.FileName);
string extension = Path.GetExtension(model.ImageFile.FileName);
fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
model.ImageURL = "~/Image/" + fileName;
fileName = Path.Combine(Server.MapPath("~/Image/"), fileName);
model.ImageFile.SaveAs(fileName);
query = new InformationConsentAndSheet()
{
ExamID = id,
ImageURL = model.ImageURL,
InformationConsentForm = model.InformationConsentForm,
InformationSheetText = model.InformationSheetText
};
ctx.InformationConsentAndSheets.Add(query);
ctx.SaveChanges();
}
return RedirectToAction("EditExam2");
}
}
我对代码的错误感到非常困惑,由于我对MVC还是陌生的,任何形式的帮助都将不胜感激。
答案 0 :(得分:0)
首先,您需要在编辑时发送调查ID。只需通过表单中的路由参数即可轻松完成此操作。
@using (Html.BeginForm("InformationSheet", "ExamAdmin", new { id = ViewBag.CurrentExamID }, FormMethod.Post, new { enctype = "multipart/form-data" }))
您用于编辑现有项目的代码有误。很好,您需要修改现有的InformationConsentAndSheet而不是创建新的。
InformationConsentAndSheet query = ctx.InformationConsentAndSheets.Where(x => x.ExamID.Equals(id)).FirstOrDefault();
if (query != null)
{
// work with files
query.ExamID = id;
query.ImageURL = model.ImageURL;
query.InformationSheetText = model.InformationSheetText;
query.InformationConsentForm = model.InformationConsentForm;
ctx.Entry(query).State = EntityState.Modified;
ctx.SaveChanges();
}
在提交更改之前,您需要指定查询已被编辑并需要保存。
希望它能提供帮助。