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

4
  • 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 :-) Commented Sep 29, 2014 at 15:23
  • i thought of using a partial view but the common fields need to display alongside the uncommon ones. see gliffy.com/go/publish/image/6238620/L.png. How do I do this with a partial view? Commented Sep 29, 2014 at 15:37
  • I can't understand the problem, could you explain me it in more details? Commented Sep 29, 2014 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 Commented Sep 29, 2014 at 19:39

1 Answer 1

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");
}
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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): microsoftvirtualacademy.com/training-courses/… microsoftvirtualacademy.com/training-courses/… and pluralsight.com/training/… IT REALLY WORTH! :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.