首页 新闻 会员 周边 捐助

MVC3 多个model从view返回,取不到值?

0
悬赏园豆:50 [已解决问题] 解决于 2013-09-23 18:57

多个model时,可以通过在viewModel中添加各个model的属性,将多个model传到view里并能显示值,但却不能把这各个model返回给Controllers?请问,如何处理啊??

问题补充:

好吧,我把源码贴上:

有两个model类,其中一个是Address

public class Address : ValueObject<Address>
    {
        #region 属性
        public string City { get; private set; }
        public string ZipCode { get; private set; }
        public string AddressLine1 { get; private set; }
        public string AddressLine2 { get; private set; }

        #endregion

        #region 构造函数

        public Address(string city, string zipCode, string addressLine1, string addressLine2)
        {
            this.City = city;
            this.ZipCode = zipCode;
            this.AddressLine1 = addressLine1;
            this.AddressLine2 = addressLine2;
        }
        public Address() { }

        #endregion
    }

另一个是Customer类

public class Customer : IValidatableObject
    {
        #region 属性
        /// <summary>
        /// 客户ID
        /// </summary>
        public string CustomerId { get; set; }

        /// <summary>
        /// 姓名1
        /// </summary>
        [Required(ErrorMessage = "请输入First Name")]
        public string FirstName { get; set; }

        /// <summary>
        /// 姓名2
        /// </summary>
        [Required(ErrorMessage = "请输入Last Name")]
        public string LastName { get; set; }

        /// <summary>
        /// 全名
        /// </summary>
        public string FullName
        {
            get
            {
                return string.Format("{0}, {1}", this.LastName, this.FirstName);
            }
            set { }
        }
        //[RegularExpression("")]
        /// <summary>
        /// 电话
        /// </summary>
        [Required(ErrorMessage = "请输入电话号码!")]
        public string Telephone { get; set; }

        /// <summary>
        /// 公司名称
        /// </summary>
        [StringLength(20, ErrorMessage = "只允许输入20个字符")]
        public string Company { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public decimal CreditLimit { get; set; }
       

        public string CountryId { get; set; }

        public Address Address { get; set; }
        #endregion
    }

EditCustomerVM是ViewModel类

public class EditCustomerVM
    {
        public Customer Customer { get; set; }

        
        /// <summary>
        /// 公司
        /// </summary>
        public string Company { get; set; }
    }

接下来是cshtml页面:EditCustomer.cshtml

@using Presentation.Web.Core
@model  Presentation.Web.ViewModels.CustomerViewModels.EditCustomerVM
@{
    ViewBag.Title = "EditCustomer";
    if (IsAjax)
    {
        Layout = null;
    }
}
<div class="ui-frame-corner">
    <li><span style="display: block;margin-left:10px;">编辑客户</span></li>
        <div class="accordion_content" id="query_form">
        @using (Html.BeginForm("EditCustomer", "Customer", FormMethod.Post, new { id = "form1" }))
        {
             @Html.HiddenFor(m => m.Customer.CustomerId)
            <table width="100%" border="0" cellpadding="0" cellspacing="0" class="main_form">
                <tr>
                    <td class="main_form_td-align1 main_form_td-width1"></td>
                    <td class="main_form_td-align2">
                        @Html.TextBoxFor(m => m.Customer.FirstName)
                        @Html.ValidationMessageFor(m => m.Customer.FirstName)
                    </td>
                    <td class="main_form_td-align3"></td>
                    <td class="main_form_td-align4">
                        @Html.TextBoxFor(m => m.Customer.LastName)
                        @Html.ValidationMessageFor(m => m.Customer.LastName)
                    </td>
                </tr>
                 <tr>
                    <td class="main_form_td-align1 main_form_td-width1">
                     电话号码
                    </td>
                    <td class="main_form_td-align2">
                        @Html.TextBoxFor(m => m.Customer.Telephone)
                        @Html.ValidationMessageFor(m => m.Customer.Telephone)
                    </td>
                    <td class="main_form_td-align3">
                       公司
                    </td>
                    <td class="main_form_td-align4">
                        @Html.TextBoxFor(m => m.Customer.Company)
                        @Html.ValidationMessageFor(m => m.Customer.Company)
                    </td>
                </tr>
                <tr>
              <td class="main_form_td-align3">
                    城市:
                </td>
                <td class="main_form_td-align4">
                    @Html.TextBoxFor(m => m.Customer.Address.City)
                </td>
                <td class="main_form_td-align3">
                    邮编:
                </td>
                <td class="main_form_td-align4">
                    @Html.TextBoxFor(m => m.Customer.Address.ZipCode, new { @class = "form_input" })
                </td>
            </tr>
            <tr>
              <td class="main_form_td-align3">
                    街道1:
                </td>
                <td class="main_form_td-align4">
                    @Html.TextBoxFor(m => m.Customer.Address.AddressLine1, new { @class = "form_input" })
                </td>
                <td class="main_form_td-align3">
                    街道2:
                </td>
                <td class="main_form_td-align4">
                    @Html.TextBoxFor(m => m.Customer.Address.AddressLine2, new { @class = "form_input" })
                </td>
            </tr>
            
            </table>
            <div class="button_box">
                <table align="center" cellpadding="0" cellspacing="0" class="accordion_content_2buttons">
                    <tr>
                        <td align="center" valign="middle">
                            <table border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td>
                                    <input id="btnSubmit" type="button" class="btn-save-icon" value="保  存"/>
                                    </td>
                                <td><input id="btnReturn" type="button" class="btn-return-icon" value="返  回"/>
                                </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </div>
        }
    </div>
</div>
<script type="text/javascript">
    $(function () {
         $("#btnSubmit").bind('click', function () {
            var form = $("#form1");
            if (form.length > 0) {
                form.submit();
            }
        });
        //返回按钮Click事件
        $("#btnReturn").bind('click', function () {
            window.location.href = "@Url.Action("QueryCustomer")";
        });
    });
   
   
  
</script>

以下是CustomerController,部分代码如下


 /// <summary>
        /// 得到单条记录数据(这个方法中Address是有值的)
        /// </summary>
        /// <param name="customerId"></param>
        /// <returns></returns>
        public ActionResult EditCustomer(string customerId)
        {
            Customer customer = _ICustomerRepository.Get(customerId);
            EditCustomerVM vm = new EditCustomerVMBuilder().Bind();
            vm.Customer = customer;
            vm.Customer.Address = customer.Address;
            vm.Company = customer.Company;
           
            vm.LovesList = CommonModelBuilder.LovesList();
            return View(vm);


        }

        }

有问题的方法:

/// <summary>
        /// 编辑数据方法
        /// </summary>
        /// <param name="formCollection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditCustomer( FormCollection formCollection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Customer _Customer = new Customer();
                    Address _Address = new Address();

                    UpdateModel(_Customer, "Customer");
                    UpdateModel(_Address, "Address");
                    
                    _ICustomerRepository.Update(_Customer);
                    _ICustomerRepository.UnitOfWork.Commit();
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("提交数据错误", ex);
            }
            return RedirectToAction("QueryCustomer");
        }

就是在以上方法中,在_Customer中的Address值都是null,请问各大侠,是什么原因啊??

无名草的主页 无名草 | 初学一级 | 园豆:134
提问于:2013-07-31 22:49
< >
分享
最佳答案
0

问题原因已找到:是因为Address中的属性设置了private set 了,多谢各位大侠关注,谢谢。

无名草 | 初学一级 |园豆:134 | 2013-08-25 16:42
其他回答(5)
0

贴点代码~

看你的描述,这样的写法应该是不能自动装配的。

幻天芒 | 园豆:37205 (高人七级) | 2013-08-01 00:11
1

创建一个类 A,把多个model组合起来,然后 以A这个类作为 view和controllers之间的model进行传值

Yu | 园豆:12990 (专家六级) | 2013-08-01 08:23
1

这个提问本身就是问题,怎么会存在从View中将Model返回给Controller的情况?

dudu | 园豆:30925 (高人七级) | 2013-08-01 14:53

如修改一条数据,是需要将页面的值传给Controller的呀

支持(0) 反对(0) 无名草 | 园豆:134 (初学一级) | 2013-08-25 11:14
0

????你要提交到各自指定的controller和action

[秦时明月] | 园豆:738 (小虾三级) | 2013-08-02 15:58
1

一个view对应一个viewmodel

收获园豆:50
小兵仔 | 园豆:1240 (小虾三级) | 2013-08-06 17:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册