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 = "bclairmont@harrisworld.com";
        //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 为 0,即使 ti 在视图加载时默认为 1。

有任何想法吗?

EDIT_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _ 我查看了 Chrome 的 DevConsole,在网络下我可以看到我的帖子请求。以下是它包含的详细信息。在我看来,数据确实已发送到服务器,所以我不知道为什么服务器无法填写我的模型类

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:bclairmont@harrisworld.com
BCCAddresses:adunn@harrisworld.com,bclairmont@harrisworld.com,
bkelly@harrisworld.com,bhackett@harrisworld.com,jwade@harrisworld.com,
krichter@harrisworld.com,mroy-waters@harrisworld.com,
nburckhard@harrisworld.com,rlibby@harrisworld.com
Subject:Testing
Body:

这是从客户端到服务器来回传递的类,以防万一

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;
}
4

2 回答 2

0

1)您是否尝试过指定模型将提交到的控制器的路由?我的意思是,像这样声明表格:

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

2) 为什么不创建一个简单的“获取”操作来返回强类型视图和一个“发布”操作,该操作接收与您在视图中添加的信息相同的模型。一旦你完成了这项工作,你就可以开始添加额外的代码,这样就很容易解决问题。

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

4) 您是否配置了可以使您的帖子被重定向到另一个区域、控制器或操作的路由规则?

于 2013-01-18T23:14:16.280 回答
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; }
}
于 2013-01-19T06:30:50.337 回答