I want to change the value of an enumdropdownlist from active to inactive in my database by doing some ajax call WITHOUT refreshing my page? How do I do this? javascript method or ajax.beginform? not sure here...
I tried ajax.beginform and the call gets made the to the controller ok, but then it tries to refresh/render a view by returning it as a actionresult. I don't want it to refresh the view because I loose my viewmodel data. I thought ajax.beginform only refreshed what was inside the form? Do I need to perform this in a javascript method instead? What do I do to prevent a view from being refreshed/rendered in my action method?
here is my ajax form. If I return a view I loose all the 'Model' view data, so model.statusenabled is null! I don't understand why its null because it's outside of the ajaxform...
@if (Model.StatusEnabled)
{
using (Ajax.BeginForm("UpdateStatus", "Student", new AjaxOptions
{
HttpMethod = "post",
OnSuccess = "dotcolor();"
}))
{
@Html.EnumDropDownListFor(model => model.Status, new { @class = "btn btn-default btn-lg dropdown-toggle", onchange = "this.form.submit();", id = "enumstatus" })
}
}
else
{
@Html.EnumDropDownListFor(model => model.Status, new { @class = "btn btn-default btn-lg dropdown-toggle", disabled = "disabled" })
}
here is my action method
[HttpPost]
public ActionResult UpdateStatus()
{
//update database
// don't return view because it gets refreshed
// and I have to re pass in my viewmodel
return View("Edit");
}
if I change the return type to void in UpdateStatus() it still attemptsto return a view called UpdateStatus. Not what I want.
Ajax.BeginForm
refreshes (replaces, perpends or appends) the contents of the element to specify in theUpdateTargetId
of theAjaxOptions
(which you have not specified). But youif
block does not make sense - theelse
statement renders a<select>
outside the form - which makes me think all this is enclosed in another form element (nested forms are invalid and not supported) – Stephen Muecke Feb 18 at 23:34