Asp.net-mvc – Pass ViewBag Values from Razor View to Controller using HTML.Begin

asp.net-mvcasp.net-mvc-3razorviewbag

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 for BeginForm is for htmlAttributes. Check your HTML code and you'll probably see an attribute on the form named Sku. These attributes don't get posted to the server.

You need to create a form element to contain your data to be posted.

@Html.Hidden("Sku", @ViewBag.SkuCategory);