我正在尝试更新表中的记录,但是当我提交记录时,将其设置为Null。
我想发生的事情是拥有一个多表单视图页面,我可以在其中编辑我的ParentModel 以及我模型中的记录
我有一个适用于Parentmodel的提交按钮 并为Model1s中的每个Recod提供一个提交按钮
但是每次我提交任何model1s记录时,调试都会说我的模型为空
我对ASP.Net相当陌生,并且已经从该论坛中进行了大量搜索, 我希望你能帮助我
欢呼
查看模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class CombineModel
{
public ParentModel ParentModel { get; set; }
public IEnumerable<Model1> Model1s { get; set; }
}
}
控制器
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class CombineModelController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult Details(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
CombineModel CombineModel = new CombineModel();
CombineModel.ParentModel = db.ParentModel.Find(id);
CombineModel.Model1s = db.Model1.Where(x => x.ParentId.Equals(id));
if (CombineModel == null)
{
return HttpNotFound();
}
return View(CombineModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Details(CombineModel model)
{
if (ModelState.IsValid)
{
db.Entry(model.ParentModel).State = EntityState.Modified;
db.SaveChanges();
}
CombineModel CombineModel = new CombineModel();
CombineModel.ParentModel = db.ParentModel.Find(model.ParentModel.ParentId);
CombineModel.Model1s = db.Model1.Where(x => x.ParentId.Equals(model.ParentModel.ParentId));
if (CombineModel == null)
{
return HttpNotFound();
}
return View(CombineModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditChild([Bind(Prefix = "Model1")] Model1 model)
{
db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Details", "CombineModel", new { id = model.ParentId });
}
}
}
查看
@model WebApplication1.Models.CombineModel
@{
ViewBag.Title = "Details";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Combine Model</h4>
<hr />
@Html.HiddenFor(model => model.ParentModel.ParentId)
<div class="form-group">
@Html.LabelFor(model => model.ParentModel.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ParentModel.Name, 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="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<table class="table">
<tr>
<th>
Name
</th>
</tr>
@foreach (var item in Model.Model1s)
{
<tr>
<td>
@using (Html.BeginForm("EditChild", "CombineModel", FormMethod.Post, new { }))
{
@Html.AntiForgeryToken()
@Html.EditorFor(modelItem => item.Id)
@Html.EditorFor(modelItem => item.ParentId)
@Html.EditorFor(modelItem => item.Name)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save-2" class="btn btn-default" />
</div>
</div>
}
</td>
</tr>
}
</table>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}