我在这里有 mvc3 应用程序我的问题在同一页面上,即在Index.cshtml
我有两个部分视图controlpartial.cshtml
和webgridpartial.cshtml
现在,当我将数据插入数据库时,它工作正常,页面被重定向到Index.cshtml
并且 URL 也变得清晰。
但是在编辑时,当我单击编辑按钮并更新所有值并提交该值时,它已在数据库中完美更新,但页面无法再次重定向到,Index.cshtml
也无法清除我的 URL :(
因此,一旦完成编辑,我将无法编辑任何值,需要手动重置索引 URL。
有什么猜测吗?
索引.cshtml
@model Mapping.Models.SecurityIdentifierMappingViewModel
@{
ViewBag.Title = "Index";
}
指数
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@*Partial control for all controls*@
<div id="main">
<div style="float:left;width:30%">
@Html.Partial("_ControlsPartial",Model)
</div>
<div style="margin-left:600px;margin-top:-20px">
@Html.Partial("_WebGridPartial",Model.MappingWebGridList)
</div>
</div>
家庭控制器.cs
mydataEntities dbContext = new mydataEntities();
DataRepository objRepository = new DataRepository();
//GET
public ActionResult Index(string userAction="Create",int id = 0 )
{
var mappingobj = new SecurityIdentifierMappingViewModel();
mappingobj.MappingWebGridList = dbContext.SecurityIdentifierMappings.ToList();
if (id!=null && id > 0)
{
mappingobj.MappingControls = dbContext.SecurityIdentifierMappings.Find(id);
if (userAction == "Delete")
{
dbContext.SecurityIdentifierMappings.Remove(mappingobj.MappingControls);
dbContext.SaveChanges();
}
}
else
{
mappingobj.MappingControls = new SecurityIdentifierMapping();
mappingobj.MappingControls.PricingSecurityID = 0;
mappingobj.MappingControls.CUSIP = "";
}
mappingobj.PricingSecurities = objRepository.GetPricingSecurityID();
mappingobj.CUSIPs = objRepository.GetCUSIP();
return View(mappingobj);
}
//POST
[HttpPost]
public ActionResult Index(SecurityIdentifierMappingViewModel objModel)
{
//edit code
if (objModel.MappingControls.Id > 0)
{
if (ModelState.IsValid)
{
dbContext.Entry(objModel.MappingControls).State = EntityState.Modified;
try
{
dbContext.SaveChanges();
return RedirectToAction("Index");
}
catch (System.Data.Entity.Validation.DbEntityValidationException ex)
{
throw;
}
}
}
//insert code
else
{
if (ModelState.IsValid)
{
dbContext.SecurityIdentifierMappings.Add(objModel.MappingControls);
try
{
dbContext.SaveChanges();
return RedirectToAction("Index");
}
catch (System.Data.Entity.Validation.DbEntityValidationException ex)
{
throw;
}
}
}
objModel.MappingWebGridList = dbContext.SecurityIdentifierMappings.ToList();
objModel.PricingSecurities = objRepository.GetPricingSecurityID();
objModel.CUSIPs = objRepository.GetCUSIP();
objModel.MappingControls = new SecurityIdentifierMapping();
return View(objModel);
}