0

I have a simple model called JobStatus, and I am enabling edit functionality within the Index view with an Ajax Actionlink.

My problem is that if there is a model error when editing an item, I don't know how to bring the item back to the model.

My Index action in my controller:

public ActionResult Index()
{
    return View(db.JobStatuses.OrderBy(x => x.JobStatusID).ToList());
}

Here is my Index view:

@model IEnumerable<Project.Models.JobStatus>

@foreach (var item in Model)
{
<div id='@string.Format("div_{0}", item.JobStatusID)'>
    @item.JobStatusID
    @item.Name
    @item.Description
    @Ajax.ActionLink("Edit", "Edit", new { id = item.JobStatusID }, new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = string.Format("div_{0}", item.JobStatusID) })
</div>
}

And here is my edit GET request:

public ActionResult Edit(string id = null)
{
    JobStatus jobstatus = db.JobStatuses.Find(id);
    if (jobstatus == null)
    {
        return HttpNotFound();
    }
    return PartialView(jobstatus);
}

My edit.cshtml:

@model Project.Models.JobStatus

@using Microsoft.Ajax;

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.JobStatusID)
    @Html.TextBoxFor(model => model.Name, null, new { @class = "form-control" })
    @Html.TextBoxFor(model => model.Description, null, new { @class = "form-control" })
    <input type="submit" value="Save"  />

}

And finally my edit POST method:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(JobStatus jobstatus, string action)
{
    if (ModelState.IsValid)
    {
        db.Entry(jobstatus).State = EntityState.Modified;
        db.SaveChanges();           
        return RedirectToAction("Index");
    }
    // Not sure what to do here, how to I repopulate my index form?
    return RedirectToAction("Index");
}

At the moment, on a modelstate failure the user is just redirected to the index page - what I'd like is to simply redisplay the index form, with the appropriate edit form enabled and populated, and any validation errors shown.

I've tried redirecting to my Edit action, but this just shows my form on a new page, without my index form (because currently my edit action is an ajax action)

Of course, please let me know if there is a better way to achieve this!

Evonet
  • 3,600
  • 4
  • 37
  • 83

1 Answers1

0

You can't do the RedirectToAction with Ajax Post.

Check out asp.net mvc ajax post - redirecttoaction not working

But you can load in the following way

[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult Edit(JobStatus jobstatus, string action)
{
    if (ModelState.IsValid)
    {
        db.Entry(jobstatus).State = EntityState.Modified;
        db.SaveChanges();           
        return RedirectToAction("Index");
    }

    return Json(new{id=jobStatus, success=true})
}

Once you get the JSON result from server you can load the same using the id returned

Like

$.post('/jobs/edit',{data:$(form).serialize()},function(data){
if(data.success){
  $.get('/jobs/index/'+data.id,{},function(data){
   $('#container').html(data);
  });
}

});
Community
  • 1
  • 1
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120