Reputation: 16239
I have mvc3 application here my problem is on same page i.e on Index.cshtml
i have two partial view controlpartial.cshtml
and webgridpartial.cshtml
now when i'm inserting data into database it works fine and page gets redirect to Index.cshtml
and URL also gets clear.
But at the time of edit when i clicked on edit button and update all values and submit that values it is updated perfectly in database but page is not able to redirect again to Index.cshtml
nor my URL gets cleared :(
Because of that I'm not able to edit any values once i done edit need to manually reset Index URL.
Any guesses?
Index.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>
HomeController.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);
}
Upvotes: 0
Views: 3078
Reputation: 24759
Recommend instead of
return View(objModel);
to do
return RedirectToAction("Index", new {Id = null});
This is called Post-Redirect-Get model (PRG).
If you wish to keep objModel. Place it in TempData and on Index(Get), to check TempData if the object is there. To use that instead of loading a new one.
Upvotes: 2
Reputation: 1189
You would need a RedirectToAction somewhere in your code to take it back to the original index.
Plus, you're doing it a little bit wrong. The whole point of MVC is that your actions would (mostly) correspond to CRUD. Ergo, you would have an Edit action. You would also have Create and Delete. Obviously, you can have more or less or even different actions, but the whole point of MVC is to make it simpler, not harder.
Routing and actions are there for a reason, to avoid writing monolithic code. I would strongly suggest you to take a look at http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3
Even though it would appear that you know the basics of ASP.NET, ASP.NET MVC is a little bit different beast :)
Upvotes: 1