简体   繁体   中英

Trying to return to same view and bind data there after posting data and binding value in asp.net mvc from same view

I am using FormMethod.post and for retrieving value on page and then post it.

index_shift.cshtml :

@using (Html.BeginForm("Index_shift", "Scm_Mod_Sug", FormMethod.Post))
{
    <div>
    @Html.DropDownList("shift_details", ViewBag.shift_details as SelectList, new { Class = "form-control selectpicker show-tick" })
    </div>
}

In controller Scm_Mod_Sug

public ActionResult Index_shift()
{
    ViewBag.shift_details = new SelectList(Getshift_details(), "ShiftVal", "ShiftVal");
    return View();
}

After posting value to it want to return value to same view ( index_shift.cshtml ) and bind value on that page but I get an error

There is no ViewData item of type 'IEnumerable' that has the key 'shift_details'

Code:

[HttpPost]
public ActionResult Index_shift(FormCollection form)
{
    shift_details = form["shift_details"];
 
    ViewBag.Rval0 = values[0].Split('~');
    return view()
}

Any idea how to achieve it would be appreciated.

Check this line:

ViewBag.shift_details =new SelectList(Getshift_details(), "ShiftVal", "ShiftVal");

You are writing value two twice. Change it to this:

ViewBag.shift_details =new SelectList(Getshift_details(), "ShiftId", "ShiftVal");

You must provide Id to the SelectList.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM