How can I pass a value in ViewBag to controller via HTML.BeginForm()
?
@using (Html.BeginForm("SaveServiceItemCategory", "Admin",
FormMethod.Post, new { Sku = @ViewBag.SkuCategory }))
{
<span>Service Item Sku : @ViewBag.SkuCategory</span>
}
Controller
public ActionResult SaveServiceItemCategory(FormCollection formCollection,
string Sku)
{}
I am getting Sku as null, and not the value in ViewBag.
How can this be solved?
Best Answer
There is no variant of
BeginForm
that accepts values to be posted.The
IDictionary
collection parameter forBeginForm
is forhtmlAttributes
. Check your HTML code and you'll probably see an attribute on the form namedSku
. These attributes don't get posted to the server.You need to create a form element to contain your data to be posted.