2013-01-18 92 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方法上的類。 –

相關問題