2013-01-18 51 views
0

我有一个表单没有收到任何关于回传的模型信息。我试图越来越多地发表评论,以使其变得简单,所以我可以看到它何时有效,到目前为止我没有运气。我已经评论了大部分形式和模型的复杂部分,所以我不知道我为什么会遇到问题。回传不包含模型数据

下面是控制器的功能展现形式,并张贴

public ActionResult MassEmail() 
    { 
     IEmailTemplateRepository templates = new EmailTemplateRepository(); 
     IEmailFromAddressRepository froms = new EmailFromAddressRepository(); 
     IEmployeeRepository emps = new EmployeeRepository(); 
     List<ProductVersion> vers = new List<ProductVersion>(); 
     MassEmailViewModel vm = new MassEmailViewModel(); 

     vers = productVersionRepository.All.OrderBy(o => o.Description).ToList(); 

     foreach (Employee e in emps.Employees.Where(o => o.Department == "Support" || o.Department == "Professional Services").OrderBy(o => o.Name)) 
     { 
      if (e.Email != null && e.Email.Trim() != "") 
      { 
       vm.BCCAddresses = vm.BCCAddresses + e.Email + ","; 
      } 
     } 
     if (vm.BCCAddresses != "") 
     { 
      vm.BCCAddresses = vm.BCCAddresses.Substring(0, vm.BCCAddresses.Length - 1); 
     } 

     ViewBag.PossibleCustomers = customerRepository.All.OrderBy(o => o.CustomerName); 
     ViewBag.PossibleTemplates = templates.All.OrderBy(o => o.Description); 
     ViewBag.PossibleFromAddresses = froms.All.OrderBy(o => o.Description); 
     ViewBag.PossibleClasses = scheduledClassRepository.All.OrderByDescending(o => o.ClassDate).ThenBy(o => o.ClassTopic.Description); 

     vm.CCAddresses = "[email protected]"; 
     //vm.Attachments = ""; 
     vm.Body = ""; 
     vm.Subject = ""; 
     vm.ToAddresses = ""; 
     vm.EmailFromAddressID = 1; 

     return View(vm); 
    } 

    [HttpPost] 
    public ActionResult MassEmail(MassEmailViewModel vm) 
    { 
     IEmailFromAddressRepository froms = new EmailFromAddressRepository(); 

     System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); 

     message.From = new System.Net.Mail.MailAddress(froms.Find(vm.EmailFromAddressID).Email); 

     string[] toAddresses = vm.ToAddresses.Split(','); 
     for (int i = 0; i < toAddresses.GetUpperBound(0); i++) 
     { 
      message.To.Add(new System.Net.Mail.MailAddress(toAddresses[i])); 
     } 

     string[] CCAddresses = vm.CCAddresses.Split(','); 
     for (int i = 0; i < CCAddresses.GetUpperBound(0); i++) 
     { 
      message.To.Add(new System.Net.Mail.MailAddress(CCAddresses[i])); 
     } 

     string[] BCCAddresses = vm.BCCAddresses.Split(','); 
     for (int i = 0; i < BCCAddresses.GetUpperBound(0); i++) 
     { 
      message.To.Add(new System.Net.Mail.MailAddress(BCCAddresses[i])); 
     } 
     message.IsBodyHtml = true; 
     message.BodyEncoding = Encoding.UTF8; 
     message.Subject = vm.Subject; 
     message.Body = vm.Body; 

     for (int i = 0; i < Request.Files.Count; i++) 
     { 
      HttpPostedFileBase file = Request.Files[i]; 
      message.Attachments.Add(new Attachment(file.InputStream, file.FileName)); 
     } 

     System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(); 
     client.Send(message); 

     return RedirectToAction("MassEmail"); 
    } 

接下来是我的视图代码

@model TRIOSoftware.WebUI.Models.MassEmailViewModel 

@{ 
    ViewBag.Title = "MassEmail"; 
} 

@using (Html.BeginForm()) 
{ 
    <h1 class="align-right">Mass E-Mail</h1> 

    <br /> 
    <br /> 

<div> 
<div class="editor-label" style="float:left; width:90px"> 
    From 
</div> 
<div class="editor-field" style="float:left"> 
    @Html.DropDownListFor(model => model.EmailFromAddressID, 
((IEnumerable<TRIOSoftware.Domain.Entities.EmailFromAddress>) 
ViewBag.PossibleFromAddresses).OrderBy(m => m.Description).Select(option => new 
SelectListItem 
{ 
    Text = option.Description.ToString(), 
    Value = option.ID.ToString(), 
    Selected = (Model != null) && (option.ID == Model.EmailFromAddressID) 
}), "Choose...") 
</div> 
</div> 

<div class= "TagitEmailAddress" style="width:100%"> 
<div class="editor-label" style="float:left; clear:left; width:90px"> 
    To 
</div> 
<div class="editor-field" style="float:left; width:88%"> 
    @Html.TextBoxFor(model => model.ToAddresses, new { @class = "TagTextBox" }) 
</div> 
</div> 

<div class= "TagitEmailAddress" style="width:100%"> 
<div class="editor-label" style="float:left; clear:left; width:90px"> 
    CC 
</div> 
<div class="editor-field" style="float:left; width:88%"> 
    @Html.TextBoxFor(model => model.CCAddresses, new { @class = "TagTextBox" }) 
</div> 
</div> 

<div class= "TagitEmailAddress" style="width:100%"> 
<div class="editor-label" style="float:left; clear:left; width:90px"> 
    <input type="button" id="BCC" value="BCC" class="btn"/> 
</div> 
<div class="editor-field" style="float:left; width:88%"> 
    @Html.TextBoxFor(model => model.BCCAddresses, new { @class = "TagTextBox" }) 
</div> 
</div> 

<div style="width:100%"> 
<div style="float:left; clear:left; width:90px"> 
    <input type="button" id="Subject" value="Subject" class="btn"/> 
</div> 
<div style="float:left; width:88%"> 
    @Html.TextBoxFor(model => model.Subject, new { id = "SubjectText", style = 
    "width:100%" }) 
</div> 
</div> 

<div style="width:100%"> 
<div style="clear:left; float:left; width:100%;"> 
    <div class="editor-field" style="float:left; width:100%;"> 
     @Html.TextAreaFor(model => model.Body, new { id = "BodyText" }) 
    </div> 
</div> 
</div> 

<br /> 
<br /> 
<br /> 

<p style="clear:both"> 
    <input type="submit" value="Send E-Mail" class="btn btn-primary"/> 
</p> 

<div id="DefaultEmailText"> 
<div class="editor-label" style="float:left; width:150px"> 
    E-Mail Template 
</div> 
<div class="editor-field" style="float:left; padding-left:10px"> 
    @Html.DropDownList("EmailTemplate", 
    ((IEnumerable<TRIOSoftware.Domain.Entities.EmailTemplate>) 
    ViewBag.PossibleTemplates).Select(option => new SelectListItem 
    { 
     Text = option.Description, 
     Value = option.ID.ToString(), 
     Selected = false 
    }), "Choose...", new { ID = "Template", style = "width:200px" })  
</div> 
</div> 
} 

@section sidemenu { 
    @Html.Action("EmailsSideMenu", "Admin") 
} 

<script type="text/javascript"> 
var TemplateSubject = ""; 
var TemplateBody = ""; 

$(document).ready(function() { 

    $('#attach').MultiFile({ 
     STRING: { 
      remove: '<i style="color:Red" class="icon-remove-sign"></i>' 
     } 
    }); 

    $(".TagTextBox").tagit(); 

    $("#BodyText").cleditor({ 
     width: 800, 
     height: 400 
    }); 

    $("#DefaultEmailText").dialog({ 
     autoOpen: false, 
     height: 150, 
     width: 250, 
     title: "Default Subject/Body", 
     modal: true, 
     buttons: { 
      OK: function() { 
       var selectedTemplate = $("#DefaultEmailText #Template").val(); 
       if (selectedTemplate != null && selectedTemplate != '') { 
        $.getJSON('@Url.Action("GetTemplate", "EmailTemplates")', { id: 
         selectedTemplate }, function (template) { 
         $("#SubjectText").val(template[0].Subject); 
         $("#BodyText").val(template[0].Body).blur(); 
        }); 
       } 
       $(this).dialog("close"); 
      }, 
      Cancel: function() { 
       $(this).dialog("close"); 
      } 
     } 
    }); 

    $('#Subject').click(function() { 
     $("#DefaultEmailText").dialog("open"); 
    }); 


}); 

</script> 

当我提交我得到除EmailFromAddressID所有空值即使ti在视图加载时默认为0,也是0。

任何想法?

EDIT_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _ 我看着D Chrome的evConsole和网络下我看到我的帖子请求。以下是它所包含的详细信息。在我看来liek数据没有被发送到服务器,所以我不knwo服务器为什么不能在我的模型类

Request URL:http://localhost:53730/Customers/MassEmail 
Request Headersview source 
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Content-Type:application/x-www-form-urlencoded 
Origin:http://localhost:53730 
Referer:http://localhost:53730/Customers/MassEmail 
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) 
Chrome/24.0.1312.52 Safari/537.17 

Form Dataview sourceview URL encoded 
EmailFromAddressID:1 
ToAddresses: 
CCAddresses:[email protected] 
BCCAddresses:[email protected],[email protected], 
[email protected],[email protected],[email protected], 
[email protected],[email protected], 
[email protected],[email protected] 
Subject:Testing 
Body: 

这里填的是获取来回传递从CLIEN TTO服务器类有帮助的情况下

public class MassEmailViewModel 
{ 
    //public MassEmailViewModel() 
    //{ 
    // ComplexQuery = new CustomerQueryViewModel(); 
    //} 

    public int EmailFromAddressID; 

    // public CustomerQueryViewModel ComplexQuery; 

    public string ToAddresses; 
    public string CCAddresses; 
    public string BCCAddresses; 
    public string Subject; 
    public string Body; 
    //public string Attachments; 
} 
+0

id字段被调回为0。它有一个默认值0。 –

+1

您应该在DevConsole(IE和Chrome F12)或FireBug或fiddler中检查您的POST请求,并检查将哪些数据发送到服务器。 – nemesv

+0

我没有检查DevConsole并发布了上面的结果。它看起来像数据正在通过,所以我猜这意味着它的问题是,它不是在自动传递回来的数据中填充类。 –

回答

0

DefaultModelBinder需要公共属性非公有领域。

更改字段属性,它应该工作:因为它的整数不能为空

public class MassEmailViewModel 
{ 
    public int EmailFromAddressID { get; set; } 

    public string ToAddresses { get; set; } 
    public string CCAddresses { get; set; } 
    public string BCCAddresses { get; set; } 
    public string Subject { get; set; } 
    public string Body { get; set; } 
} 
+0

你说得对。我改变了他们的财产,我工作。我想我有点困惑。我所有的其他观点都是基于相同类型的课程,尽管这些课程与EF有关。为什么在这些课程中,我不需要声明属性?与EF挂钩有什么关系呢? –

0

1)您是否尝试过指定控制器的模型将被submited的路线?我的意思是,声明是这样的形式:

@using (Html.BeginForm("YourAction","YourController", FormMethod.Post)) 

2)你为什么不只是创建一个返回强类型的视图,并接收与相同的信息模型中的“邮报”的行动简单的“获取”行动你在视图中添加。一旦你开始工作,你可以开始添加额外的代码,这样很容易就能解决问题。

3)确保所有助手都在表单内。

4)您是否配置了可以让您的帖子被重定向到其他区域,控制器或操作的路由规则?

+0

我确实尝试在我的表单中指定路由并获得了sema结果。我知道它被贴在corect路由上,因为我在控制器的后期例程开始处有一个断点,它在那里。我在课堂上没有数据。我甚至可以看到,实际的数据被传递给请求对象中的controllr,但它并没有转化为我的类。 –

+0

我想我没有看到你提出的建议和我正在做的事情的区别。我有正常的控制器函数返回我的强类型视图,所以我不明白为什么数据不会回发到post方法上的类。 –

相关问题