我有一个ViewModel,我试图回发以保存在数据库中.虽然ViewModel正在获取//GET Create(int id)它所设置的值,但是当它到达时它会丢失一些//POST Create ().将GoalLevel与GoalTitle正在通过确定已过,但ActorId并ProjectId正在丧失,甚至虽然调试器是通过去if (Model.State.IsValid)块,即正在经历的值没有被保存到数据库中.然后重定向会失败,因为我不再有效ProjectId传递给URL.
如果你知道你在做什么,我相信这是显而易见的!
CONTROLLER
// GET: UseCases/Create
public ActionResult Create(int id)
{
ViewBag.projectId = id;
//Populate the ViewModel
UserGoalsStepViewModel model = new UserGoalsStepViewModel()
{
ProjectId = id,
ActorList = new SelectList(db.Actors.Where(a => a.projectID == id), "id", "Title"),
};
return View(model);
}
// POST: UseCases/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ProjectId,ActorId,GoalTitle,Level")] UserGoalsStepViewModel model)
{
if (ModelState.IsValid)
{
//Create a use case object to map to, which can then be saved in the DB
UseCase uc = new UseCase();
uc.ProjectID = model.ProjectId;
uc.ActorID = model.ActorId;
uc.Level = (Level)model.Level;
db.SaveChanges();
return RedirectToAction("Index", new { id = model.ProjectId });
}
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
视图模型
public class UserGoalsStepViewModel
{
public enum GoalLevel
{
Summary, UserGoal, SubGoal,
}
public int ProjectId { get; set; }
public int ActorId { get; set; }
[DisplayName("Actor Title")]
public SelectList ActorList { get; set; }
[DisplayName("Goal Title")]
public string GoalTitle { get; set; }
public GoalLevel Level { get; set; }
public UserGoalsStepViewModel ()
{
}
}
Run Code Online (Sandbox Code Playgroud)
视图
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>UseCase</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ActorList, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.ActorList, Model.ActorList, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ActorList, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.GoalTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.GoalTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.GoalTitle, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Level, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.Level, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Level, "", 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>
}
Run Code Online (Sandbox Code Playgroud)
更新的POST方法仍然存在错误而不是DB
在修改之后,我现在Message=The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.UseCase_dbo.Actor_ActorID". The conflict occurred in database "JustSpecIt", table "dbo.Actor", column 'ID'在尝试保存到DB时遇到此错误.
这是更新的POST方法的相关位:
if (ModelState.IsValid)
{
//Create a use case object to map to, which can then be saved in the DB
UseCase uc = new UseCase();
uc.ProjectID = model.ProjectId;
uc.ActorID = model.ActorId;
uc.Title = model.GoalTitle;
uc.Level = (Level)model.Level;
db.UseCases.Add(uc);
db.SaveChanges();
return RedirectToAction("Index", new { id = model.ProjectId });
}
Run Code Online (Sandbox Code Playgroud)
您需要将ProjectId和ActorId的值存储为表单上的隐藏输入,以便它们可以包含在发送回控制器操作的已发布值中.
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(x => x.ProjectId)
@Html.HiddenFor(x => x.ActorId)
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
114 次 |
| 最近记录: |