回发包含没有模型数据

时间:2013-01-18 21:18:51

标签: asp.net-mvc asp.net-mvc-3

我的表单没有收到关于回发的任何模型信息。我试图评论越来越多,以使其简单,所以我可以看到它何时起作用,到目前为止我没有运气。我已经评论了表单和模型的大部分复杂部分,所以我不知道为什么会遇到问题。

以下是控制器功能,用于显示表单并发布

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之外的所有空值,即使在视图加载时ti被默认为1,也会为0。

有什么想法吗?

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

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

2 个答案:

答案 0 :(得分:0)

1)您是否尝试过指定模型将被提交的控制器的路径?我的意思是,宣布这样的形式:

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

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

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

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

答案 1 :(得分: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; }
}