Reputation: 3773
I have a form that creates a new slide (part of a simple CMS) and currently I declare the form using the following code:
Create:
@model Models.Slide
@{
ViewBag.Title = "Create New Slide";
Layout = "~/Areas/Admin/Views/Slide/_SlideLayout.cshtml";
}
<h2>Create Slide</h2>
@Html.Partial("_SlideForm", Model)
Slide Form
@model Models.Slide
@using (Html.BeginForm("Create", "Slide", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Slide</h4>
<hr />
....
Edit:
@model Models.Slide
@{
ViewBag.Title = "Edit New Slide";
Layout = "~/Areas/Admin/Views/Slide/_SlideLayout.cshtml";
}
<h2>Edit Slide</h2>
@Html.Partial("_SlideForm", Model)
These form is a partial view and I call this partial view within a "Create" view that calls a create method in my controller.
However, I want to have an "Edit" view that also calls this partial view to render the slide form, but I obviously want to call a different action method (i.e. an edit method not a create method).
I know I could simply redo the whole form and change the action method in the begin form call to "Edit" but this seems like a lot of unnecessary duplication. Is there a way to get what view called this partial view. I.e. is there a way I can determine if the form should use "Create" or "Edit" in the begin form call? The views themselves are called Create and Edit.
Many thanks
Upvotes: 0
Views: 140
Reputation: 218702
You may use the same HttpPost action method for both Create and Edit. The only difference between edit and Create is, For Edit, You will have an ID for which you want to edit the details. So why not use the value of id to determine whether the form is being posted from Create view or Edit view ?
public CreateOrEditVM
{
public int SlideId {set;get;}
public string SlideName {set;get;}
}
And in your controller.
public ActionResult Create()
{
return View(new CreateOrEditVM());
}
public ActionResult Edit(int id)
{
var vm = new CreateOrEditVM()'
var slide = yourService.GetSlideFromId(id);
vm.SlideId=slide.Id;
vm.SlideName = slide.Name;
return View(vm);
}
[HttpPost]
public ActionResult Create(CreateOrEditVM model)
{
if(model.SlideId==0)
{
// coming from Create form
}
else
{
//Coming from Edit form
}
// to do :Save and redirect
}
Make sure you have the SlideId value in the partial view.
@model CreateOrEditVM
@using(Html.Beginform())
{
@Html.TextboxFor(s=>s.SlideName)
@Html.HiddenFor(s=>s.SlideId)
<input type="submit" />
}
If you want, You can use the same get method for create and edit, Use a nullable integer as the param and based on the value, Return the viewmodel.
Upvotes: 0
Reputation: 1446
Just set value to your ViewBag
in each ActionMethod
.
public ActionResult Edit()
{
// Do some edit stuff
ViewBag.FormType = "Create";
return View();
}
public ActionResult Create()
{
// Do some create stuff
ViewBag.FormType = "Create";
return View();
}
And then use it in your Razor
view:
@model Models.Slide
@{
Layout = "~/Areas/Admin/Views/Slide/_SlideLayout.cshtml";
}
<h2>@ViewBag.FormType Slide</h2>
@Html.Partial("_SlideForm", Model)
Upvotes: 1
Reputation: 880
Can you pass additional parameter when you create this partial ? Something like
@using(Html.BeginForm("Partial", "Controller",
new { action = "Edit" }, FormMethod.Post, null)
Upvotes: 0