0

I am trying to reuse a partial view form between two views (edit and create).

Inside the parent views I declare a variable called form type and I basically just want to use that value in my partial view, but it doesnt seem to work. Here s what I have so far:

Parent View

@model Models.Slide
@{
    ViewBag.Title = "Edit Slide";
    ViewBag.FormType = "Edit";
    Layout = "~/Areas/Admin/Views/Slide/_SlideLayout.cshtml";
}

<h2>Edit Slide</h2>

@Html.Partial("_SlideForm", Model)

Partial VIew (_SlideForm)

@model Models.Slide

 @using (Html.BeginForm(ViewBag.Title, "Slide", null, FormMethod.Post, new  { enctype = "multipart/form-data" }))
{

I basically just want to declare something outside of the partial view that I can use for the action method within the partial view.

Any help would be greatly apppreciated.

Thanks

Pectus Excavatum
  • 3,593
  • 16
  • 47
  • 68
  • _I basically just want to declare something outside of the partial view that I can use for the action method within the partial view._ Use a [Child Action](http://stackoverflow.com/questions/12530016/what-is-an-mvc-child-action?lq=1) to do this. I made an example for this [answer](http://stackoverflow.com/questions/33538427/what-is-the-best-practice-of-passing-data-from-a-controller-to-a-view-layout/33551520#33551520). – Jasen Nov 12 '15 at 20:36
  • 2
    i don't get why you don't just declare the @using (Html.BeginForm()) in the parent view and just use the partial view to show the fields – JamieD77 Nov 12 '15 at 20:39
  • @JamieD77, agree. That would be the better approach. – Chris Pratt Nov 12 '15 at 20:39
  • I think a cleaner approach is to use same HttpPost action method for Create and Edit form post. ViewBags are dynamic and i would use those at the very minimum times possible. – Shyju Nov 12 '15 at 20:45

1 Answers1

1

What you have should work. The problem is that when you pass in parameters like this, the types need to match. Anything out of ViewBag is dynamic, so you have to cast to the right type. Also, I would imagine you goofed a bit here, and intended to use ViewBag.FormType rather than ViewBag.Title. ViewBag.Title is not a valid action name, so that's going to fail regardless.

@using (Html.BeginForm((string)ViewBag.FormType, ...
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444