将相同的模型传递给不同的视图

时间:2013-12-26 20:37:45

标签: c# asp.net-mvc-4

问题:我们有一个主视图和一些部分视图。一切都使用与主视图相同的模型。在提交主视图时,我试图渲染局部视图并尝试传递模型,但只有传递下来的属性是页面或视图上的可编辑字段。我怎样才能传递其他属性?

当前解决方法:部分视图也需要其他数据来生成电子邮件正文,但由于它没有传递下来,我们正在创建隐藏字段以将其传递下来。

主视图

 @using (Ajax.BeginForm("CommonSave", "Common", null, new AjaxOptions
   {
                HttpMethod = "Post",
                InsertionMode = InsertionMode.Replace,
                //OnSuccess = "submitFormSuccess",
                //OnFailure = "submitFormFailure"
            }, new { id = "commonForm" }))
            {
                <div>
                    <table>                            
                        <tr>
                            <td>
                                @Html.EditorFor(m => m.Name)
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input type ="submit" name="Common Save" />
                            </td>
                        </tr>
                    </table>
                </div>

控制器:

        public class CommonController : Controller
        {      
            public ActionResult Index()
            {
                CommonModel model = new CommonModel()
                {
                    Id = 3,
                    Name = "Test Name"            
                };

                return View("Common", model);
            }

            [HttpPost]
            public ActionResult CommonSave(CommonModel model)
            {
                return PartialView("CommonPartial", model);
            }

            public bool BinderSave(CommonModel model)
            {
                return true;
            }

        }

在调用主视图(Common)Index时加载。 在主视图上提交Ajaxform后,将调用actionmethod CommonSave,但传递给CommonSave的模型仅包含Name而不包含Id

我们如何在不创建隐藏字段或做任何事情的情况下将其传递下去? 我的实际模型有很多需要传递的字段。

2 个答案:

答案 0 :(得分:0)

而不是使用ajax表单我会使用ajax调用并在成功时填充字段。将按钮更改为按钮而不是提交

<input type ="button" class="btnSubmit" name="Common Save" />

然后在你的脚本标签

$('.btnSubmit').click(function () { 
    $.ajax({
        url: '@Url.Action("CommonSave", "CommonController")',
        type: "POST",
        data: { Id: $('.Id').val() }
        cache: false,
        async: true,
        success: function (result) {
            //put the partial into a div on the form
            $(".commonForm").html(result);
            //now set the fields on the partial from the model
            $('#EmailName').val('@Model.EmailName');
        }
     });
});

答案 1 :(得分:0)

我理解部分视图的需要,并会告诉你我是如何解决这个问题的,但是每个视图都应该拥有它自己的模型,它应该是。这是MVVM世界的一个宗旨:

无论如何,部分可以通过接口建立模型。使用界面定义部分视图作为其模型。例如:

接口:

public interface IPhone
{
     string PhoneNumber ( get; set; }
}

你建模:

public class MainModel : IPhone
{

      public string Name
      {
          get {  ... }
          set {  ... }
      }

      public string PhoneNumber
      {
          get {  ... }
          set {  ... }
      }
 }

关键是,只要传递给主视图的模型实现了接口(接口也可以定义另一个模型的属性),部分视图依赖于该接口,那么您需要做的就是将模型传递给部分,您可能需要将模型转换为接口,但从技术上讲,您不应该这样做。如果这有帮助,请告诉我。

相关问题