2

I want to create a model-view-controller without having if-else for individual controls, or having to duplicate these to handle different on-screen controls.
Currently I have:-

//controller

public ActionResult DisplayThing1(int thingType, string thingName){

  Thing1Model model = new Thing1Model();
  return View(model);
}

[HttpPost]
public ActionResult DisplayThing1(Thing1Model model)
{
  Save(model);
  return RedirectToAction("DisplayThing1");
}

// model

public class Thing1Model()
{
 public int type {get; set; }
 public string Name {get; set;}
}

// view

@using(Html.BeginForm(....))
{
 @Html.HiddenFor(m=>m.type);
 @Html.LabelForI(m=>m.Name);
}

I have pretty much duplicated controller for Thing2Model, the model itself is

public class Thing2Model()
{
 public int type {get; set; }
 public string Name {get; set;}
 public DateTime MyDate {get; set;}
}

A combined view would look like the following.

@using(Html.BeginForm(....))
{
 @Html.HiddenFor(m=>m.type);
 @Html.LabelForI(m=>m.Name);
 @if(type == "2")
 {
   @Html.TextBoxFor(m=>m.MyDate);
 }
}

I am looking for a better option to avoid @if as well as duplicated code

EDIT: Adding onto @W92 answer. We also need to change the model binder to support inherited models. Otherwise, in the the view for this code, MVC won't understand how to put child properties.

Polymorphic model binding

Community
  • 1
  • 1
heyNow
  • 751
  • 2
  • 14
  • 38
  • 1
    you tried to use partialView or Html.Action()? (which return PartialView). In Html.Action you can put a parameters etc. It sounds pretty good. For action which service Html.Action good's use attribute: `[ChildActionOnly()]` (read in google) or ask for more details :-) – Marek Woźniak Sep 29 '14 at 15:23
  • i thought of using a partial view but the common fields need to display alongside the uncommon ones. see http://www.gliffy.com/go/publish/image/6238620/L.png. How do I do this with a partial view? – heyNow Sep 29 '14 at 15:37
  • I can't understand the problem, could you explain me it in more details? – Marek Woźniak Sep 29 '14 at 19:28
  • can you post some example code for partial view in this scenario? Can it be done inside beginform(){}, will the partial be for the date or the name and type – heyNow Sep 29 '14 at 19:39

1 Answers1

1

I don't understand your problem exactly, but pretty good, well sorry for any mistakes.

public class Thing1Model()
{
 public int type {get; set; }
 public string Name {get; set;}
}

public class Thing2Model() : Thing1Model
{
  public DateTime MyDate {get; set;}
}

and in your View: //model2

@using(Html.BeginForm(....))
{
     @Html.PartialView("_myForm");
       @Html.TextBoxFor(m=>m.MyDate);
}

and _myForm has a model of Thing1Model with content:

 @Html.HiddenFor(m=>m.type);
 @Html.LabelForI(m=>m.Name);

but when will be in View (thing1), only use:

@using(Html.BeginForm(...))
{
 @Html.PartialView("_myForm");
}
Marek Woźniak
  • 1,666
  • 14
  • 31
  • Nice! i didn't think of that. So will 1) the controller just use the parent model to get, post? 2)Can the view be strongly typed on the parent model? – heyNow Sep 29 '14 at 20:23
  • 1
    model's using for present your data (textBox, label etc). It return's only html. Test it on the browse with f12. It's a neccesary information. I prefer for you to learn with microsoftacademy :-) Here's three great courses (one from other site): http://www.microsoftvirtualacademy.com/training-courses/introduction-to-asp-net-mvc http://www.microsoftvirtualacademy.com/training-courses/developing-asp-net-mvc-4-web-applications-jump-start and http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m6-ajax&mode=live&clip=0&course=mvc4-building IT REALLY WORTH! :-) – Marek Woźniak Sep 29 '14 at 20:29