【问题标题】:mapping fails on multiple form post映射在多个表单帖子上失败
【发布时间】:2014-07-03 19:55:24
【问题描述】:

我在提交时发布了两个表单。

在我的 MVC 应用程序中,我有 EmailReportVM 类。这个类有一个名为criteria的变量,它的类型是StatsCriteria

当我发布表单时,EmailReporVM 中的所有变量都会被映射,criteria 除外。

EmailReportVM 具有与frmSendEmail 相同的变量,并且 StatsCriteriafrmPostToEmailReports 具有相同的变量

我应该怎么做才能完成这项工作。

Caller.js

$("#frmSendEmail").validate({
    onfocusout: true,
    submitHandler: function (form) {

    //Send the data using post and put the results in a div                   
    $.post(url, $("#frmSendEmail, #frmPostToEmailReports").serialize(), function (data) {
        //do something    
    });
}

控制器

[HttpPost]
public ActionResult Create(EmailReportVM report)
{
    // Here the report object which is of type EmailReportVM has values for all the variables except criteria . 
    HttpResponseMessage response = new HttpResponseMessage();

    try
    {
        using (HttpClient client = new HttpClient())
        {
            ....................          
        }                       
    }
}

EmailReportVM.cs

public class EmailReportVM
{
    #region Constructors and Methods
    public EmailReportVM() {  }
    #endregion

    #region Properties and Fields
    public string from { get; set; }  
    public string to{ get; set; }
    public string Cc{ get; set; }
    public string Bcc{ get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
    public string Notes { get; set; }
    public StatsCriteria criteria { get; set; }
    #endregion
}

统计标准

public class StatsCriteria
{
    #region Constructors and Methods
    public StatsCriteria() { }
    public string Params { get; set; }
    public string AgentId { get; set; }
    public string LicenseNum { get; set; }
    public string AgentFullName { get; set; }
    #endregion
}

【问题讨论】:

  • 您应该为表单元素使用正确的命名来分配标准属性。请记住,这不是类的正常属性,也是类中的类对象。因此,要在此对象内设置值,您应该为您的 html 元素提供限定名称。例如。 Params 的输入控件名称 应该是 criteria.Params in html
  • 谢谢。试过了,但没有运气。
  • @BumbleBee - 我们可以看看风景吗?我很想看看这些表格是如何排列的。

标签: jquery asp.net-mvc asp.net-mvc-4 http-post model-binding


【解决方案1】:

您应该使用正确的模型绑定来为复杂对象赋值。

检查一下,如果您需要任何代码帮助,请告诉我

http://lostechies.com/jimmybogard/2011/09/07/building-forms-for-deep-view-model-graphs-in-asp-net-mvc/

【讨论】:

  • 你看过链接了吗?
猜你喜欢
  • 2011-01-30
  • 2013-09-16
  • 1970-01-01
  • 2017-07-08
  • 2011-11-17
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
相关资源
最近更新 更多