0

我在一页中有两个表格。两者都有自己的属性和提交操作,但在两种表单中都有一些共同的属性。

有没有办法使用一个视图模型来构建具有两个表单的视图,该视图模型与视图中的两个表单共享属性?

实际上我有重复的字段,我必须使用 jquery 复制两个字段中值的更改。

谢谢。

4

1 回答 1

0

you can use HtmlPrefix to achieve this.

  1. I have created a model as

    public class TestModel
    {
      [Required]
      public int Id { get; set; }
      [Required]
      public string Name { get; set; }
    }
    
  2. In Controller we need to have one GetMethod but two post method for each form, but with different BindPrefix

    public ActionResult TestLoad()
    {
        TestModel model = new TestModel();
        return View(model);
    }
    
    public ActionResult TestA([Bind(Prefix = "A")]TestModel model)
    {
        return View("TestLoad",model);
    }
    public ActionResult TestB([Bind(Prefix="B")]TestModel model)
    {
        return View("TestLoad",model);
    }
    
    1. In the view. Here also we need to Specify binding prefix
    <table>
     <tr>
      <td>
        @{Html.ViewData.TemplateInfo.HtmlFieldPrefix="A";}
        @{Html.RenderPartial("PartialA");}
      </td>
      <td>
        @{Html.ViewData.TemplateInfo.HtmlFieldPrefix="B";}
        @{Html.RenderPartial("PartialB");}
      </td>
    </tr>
    </table>
    
    1. Both partial view are exactly same , except that they post data to different actions (as defined in controller)

    2. Now if you run the project you should get appropriate output.

enter image description here

enter image description here

于 2013-03-05T07:19:44.803 回答