I have a situation where I am wanting an 'Add' View to accept an int from the controller but return a different type to the HttpPost controller method. Confusing, I know. The 'Add' View is used to create an object typed as a Widget, but I need to pass in the ID of the WidgetCategory. So in my WidgetController, I would have a method something like:
public ActionResult Add(int id) // 'id' is the WidgetCategoryID
{
return View(id);
}
However, in the view, since in it's intended to return a Widget to be added would start like this:
@using MyProject.Models
@model Widget
@{
ViewBag.Title = "My Title";
Layout = "~/Views/Shared/_MyLayout.cshtml";
}
@using (Html.BeginForm())
{
// Insert markup here ...
}
My question is, how do I pass the WidgetCategoryID into the controller if it's typed to return a Widget? I was hoping to add it as a hidden field inside the form like so:
@Html.Hidden(id)
Any ideas would be greatly appreciated.
Thanks!
idtowidgetCategoryId. If you need a comment to tell me what the variable stands for you haven't named it well.idparameter onAdd(int id)was meant forWidget.Idinstead ofWidget.WidgetCategoryIdproves my earlier point that you need to rename it instead of relying on the comment.