2014-04-12 38 views
0

我在我的代码中有这个:为什么MVC默认验证每次都被触发?

这是我的模型。

public class CompanywiseReportModel 
    { 
     [DisplayName("Company Name")] 
     public int ClientDetailId { get; set; } 

     [DisplayName("Company Name")] 
     public string CompanyName { get; set; } 

     public IEnumerable<SelectListItem> CompanyNameList { get; set; } 

     [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] 
     public DateTime FromDate { get; set; } 

     [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] 
     public DateTime ToDate { get; set; } 
    } 

这是我的看法:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <div class="divpageTitle"> 
     <asp:Label ID="lblPageTitle" runat="server" Text="Company Wise Report"></asp:Label> 
    </div> 

    <% Html.EnableClientValidation();%> 
    <% using (Html.BeginForm("GenerateCompanywiseReport", "Report", FormMethod.Post, new { target = "_blank" })) 
     { %> 
    <div id="clientdetail"> 
     <div class="clientform"> 
      <asp:Panel ID="Panel1" GroupingText="Company Wise Report" runat="server"> 
       <div class="innerForm"> 
        <div class="editor-label"> 
         <%: Html.LabelFor(model => model.ClientDetailId,"Company Name") %> 
        </div> 
        <div class="editor-field"> 
         <%: Html.DropDownListFor(model => model.ClientDetailId,Model.CompanyNameList) %> 

         <%: Html.ValidationMessageFor(model => model.ClientDetailId) %> 
        </div> 

        <div class="editor-label"> 
         <%: Html.LabelFor(model => model.FromDate,"From Date") %> 
        </div> 
        <div class="editor-field"> 
         <%: Html.TextBoxFor(model => model.FromDate ,new { @class = "datePickerControl" }) %> 
         <%: Html.ValidationMessageFor(model => model.FromDate) %> 
        </div> 

        <div class="editor-label"> 
         <%: Html.LabelFor(model => model.ToDate,"To Date") %> 
        </div> 

        <div class="editor-field"> 
         <%: Html.TextBoxFor(model => model.ToDate ,new { @class = "datePickerControl" }) %> 
         <%: Html.ValidationMessageFor(model => model.ToDate) %> 
        </div> 

        <div class="editor-label"> 
         &nbsp; 
        </div> 
        <div class="editor-label"> 
         &nbsp; 
        </div> 
        <div class="editor-field"> 
         <input type="submit" value="Generate Report" /> 
         <input type="button" value="Cancel" onclick="javascript: location.href = '/ClientDetail/Index';" /> 
        </div> 
       </div> 
      </asp:Panel> 
      <% } %> 
     </div> 
    </div> 
    <uc:warning ID="ucWarning" runat="server" /></asp:Content> 
<asp:Content ID="Content1" ContentPlaceHolderID="ScriptsSection" runat="server"> 
    <%: Scripts.Render("~/bundles/jqueryval") %> 
    <%: Scripts.Render("~/bundles/FeedbackwiseReport") %> 
</asp:Content> 

此连接的javascript:

$(function() { 
    $(".datePickerControl").datepicker({ 
     showOn: "button", 
     buttonImage: "../../images/calendar.gif", 
     buttonImageOnly: true, 
     dateFormat: 'dd/mm/yy' 
    }).datepicker('setDate', null); 

}); 

这里是我的控制器的相关代码:

public ActionResult CompanywiseReport() 
     { 
      CompanywiseReportModel objCompanywiseReportModel = new CompanywiseReportModel(); 
      FeesDetailHelper objFeesDetailHelper = new FeesDetailHelper(); 
      objCompanywiseReportModel.CompanyNameList = objFeesDetailHelper.GetCompanies(0); 
      return View(objCompanywiseReportModel); 

     } 

[HttpPost] 
     public ActionResult GenerateCompanywiseReport() 
     { 
      TrainingFeedBackDetailHelper objTrainingFeedBackDetailHelper = new TrainingFeedBackDetailHelper(); 

      LocalReport localReport = new LocalReport(); 
      ReportDataSource reportDataSource = new ReportDataSource(); 
      localReport.ReportPath = Server.MapPath("~/Views/Report/companywise.rdlc"); 
      reportDataSource.Name = "CompanywiseReportDataset"; 
      int trainingDetailId = Convert.ToInt16(Request["ClientDetailId"]); 

      String Fromdate = Request["FromDate"]; 
      String Todate = Request["ToDate"]; 
      DateTime from; 
      DateTime to; 
      if (Fromdate == null) 
      { 
       from = new DateTime(01, 01, 01); 
      } 
      else 
      { 
       if (Regex.IsMatch(Fromdate, @"^([0-9]{2})\/([0-9]{1})\/([0-9]{4})$")) 
       { 
        Fromdate = (Fromdate.Insert(3, "0")); 
       } 
       from = DateTime.ParseExact(Fromdate, "dd/MM/yyyy", CultureInfo.CurrentCulture); 
      } 
      if (Todate == null) 
      { 
       to = new DateTime(01, 01, 01); 
      } 
      else 
      { 
       if (Regex.IsMatch(Todate, @"^([0-9]{2})\/([0-9]{1})\/([0-9]{4})$")) 
       { 
        Todate = (Todate.Insert(3, "0")); 
       } 
       to = DateTime.ParseExact(Todate, "dd/MM/yyyy", CultureInfo.CurrentCulture); 
      } 

      reportDataSource.Value = objTrainingFeedBackDetailHelper.GetCompanyWiseReportData(trainingDetailId,from,to); 
      localReport.DataSources.Add(reportDataSource); 

      string reportType = "PDF"; 
      string mimeType; 
      string encoding; 
      string fileNameExtension; 

      //The DeviceInfo settings should be changed based on the reportType 
      //http://msdn2.microsoft.com/en-us/library/ms155397.aspx 
      string deviceInfo = 
      "<DeviceInfo>" + 
      " <OutputFormat>PDF</OutputFormat>" + 
      " <MarginTop>0.2in</MarginTop>" + 
      " <MarginLeft>0.2in</MarginLeft>" + 
      " <MarginRight>0.2in</MarginRight>" + 
      " <MarginBottom>0.2in</MarginBottom>" + 
      "</DeviceInfo>"; 

      Warning[] warnings; 
      string[] streams; 
      byte[] renderedBytes; 

      //Render the report 
      renderedBytes = localReport.Render(
       reportType, 
       deviceInfo, 
       out mimeType, 
       out encoding, 
       out fileNameExtension, 
       out streams, 
       out warnings); 
      //Response.AddHeader("content-disposition", "attachment; filename=NorthWindCustomers." + fileNameExtension); 
      return File(renderedBytes, mimeType); 
     } 

Opppps..My问题太长,但机智h我.. !!

我的问题是,每当我尝试使用此视图执行任何操作时,模型都会失效。我的控制器甚至没有从我的视图中获得点击。我不知道这是从哪里来的。我被卡住了,找不到我的出路。对此的任何帮助都非常明显。谢谢 ..!!

回答

1

在VS中放入if(ModelState.IsValid)行中的断点,然后检查模型中的值以查看导致错误的原因。

+0

但问题是,我的看法是不发送点击事件控制器。所以,我无法检查无效的属性。 –

+0

你可以发布控制器吗? –

+0

用控制器代码编辑问题 –

相关问题