2016-11-22 40 views
0

我有一个视图在MVC 5应用程序中有很多表单域。对于不同的请求,我使用相同的视图,但使用不同的强类型ViewModels。每个ViewModel具有完全相同的字段,但是对于每种类型的请求,所需的字段是不同的。动态分配ViewModel在运行时查看MVC 5

我的问题是我可以动态地分配一个强类型ViewModel在运行时取决于情况?我试图避免为每个场景复制相同的视图,因为无论场景出现的次数如何,视图都会保持一致。

例如,我想要分配的ViewModels之一下方的视图我已经创建,这取决于选择的场景:

public class FirstScenario_ViewModel 
{ 
    [Required] 
    string FirstValue {get; set;} 

    string SecondValue {get; set;} 
} 

public class SecondScenario_ViewModel 
{ 
    string FirstValue {get; set;} 

    [Required] 
    string SecondValue {get; set;} 
} 
+0

一种选择是使用具有条件验证属性的单个模型/视图/动作方法,例如e使用[foolproof](http://foolproof.codeplex.com/)'[RequiredIf]'或类似的类型属性。另一种方法是对模型使用“基本”类/接口,然后创建一个自定义的'ModelBinder'来在POST方法中生成具体类。 –

+0

太棒了!多谢你们!由于时间紧迫,我走了一条万无一失的路线,但这些都是很好的建议。 – bigtri

回答

0

一种选择是,通过重复使用不同的子类的视图。 Razor甚至会根据发送给视图的具体类型识别属性并填充html。

子类只是解决方案的一部分。第二部分是表单发布后需要发生的模型绑定。 您可以通过创建一个自定义模型联编程序来解决此问题,如其中一个注释中所述。 另一个方法是调整表单的动作,以便将表单发布到可处理正确视图模型类型的操作。具有多个操作的优点是,对操作实施定制验证变得微不足道。

使用多个操作可能不是最优雅的解决方案。我不确定是否有任何不良副作用。

下面是一个示例视图模型:

// This is the base class declared as the view's @model 
// This one would have all the common validations 
public class TestViewModel 
{ 
    public string ModelType { get; set; } 
    public virtual string Prop1 { get; set; } 
    public virtual string Prop2 { get; set; } 
    public virtual string Prop3 { get; set; } 
} 

public class TestViewModel1 : TestViewModel 
{ 
    [Required] 
    public override string Prop1 { get; set; } 
} 

public class TestViewModel2 : TestViewModel 
{ 
    [Required] 
    public override string Prop1 { get; set; } 
    [Required] 
    public override string Prop2 { get; set; } 
} 

而且,所述控制器:

public class TestController : Controller 
{ 
    // GET: Test 
    public ActionResult Edit(string modelType) 
    { 
     if (modelType == "1") return View(new Models.TestViewModel1() {ModelType = "1"}); 
     if (modelType == "2") return View(new Models.TestViewModel2() {ModelType = "2" }); 
     return View(new Models.TestViewModel() { ModelType = "" }); 
    } 

    [HttpPost] 
    public ActionResult Edit(Models.TestViewModel model) 
    { 
     if (model.Prop1 == null) ModelState.AddModelError("Prop1", "Please type something"); 
     if (ModelState.IsValid) return RedirectToAction("Edit"); 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Edit1(Models.TestViewModel1 model) 
    { 
     if (model.Prop1 == null || !model.Prop1.Contains("1")) ModelState.AddModelError("Prop1", "Please type at least one character 1"); 
     if (ModelState.IsValid) return RedirectToAction("Edit", new { modelType = "1" }); 
     return View("Edit", model); 
    } 

    [HttpPost] 
    public ActionResult Edit2(Models.TestViewModel2 model) 
    { 
     if (model.Prop2 == null || !model.Prop1.Contains("2")) ModelState.AddModelError("Prop2", "Please type at least one character 2"); 
     if (ModelState.IsValid) return RedirectToAction("Edit", new { modelType = "2"}); 
     return View("Edit", model); 
    } 
} 

和视图(看@using(Html.BeginForm ..):

@model WebApplication1.Models.TestViewModel 
@{ 
    Layout = null; 
} 
@Scripts.Render("~/bundles/jquery") 
@Scripts.Render("~/bundles/jqueryval") 

@using (Html.BeginForm("Edit" + Model.ModelType, "Test", new {Model.ModelType})) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>TestModel</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     @DateTime.Now.ToString("o") 
     <br/> 
     @Html.LabelFor(m => m.ModelType) 
     @Html.DisplayFor(m => m.ModelType) 
     <hr /> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Prop1, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Prop1, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Prop1, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Prop2, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Prop2, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Prop2, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Prop3, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Prop3, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Prop3, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Save" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div>