部分视图按钮提交不调用httpost,调用动作进行查看

时间:2015-04-17 13:21:47

标签: c# ajax asp.net-mvc asp.net-ajax partial-views

观点:

<div id="ReportChange">
@{
    ViewDataDictionary vDataDictonary = new ViewDataDictionary();
    vDataDictonary.Add(new KeyValuePair<string, object>("ReportType", ViewData["ReportType"]));
    vDataDictonary.Add(new KeyValuePair<string,object>("Agencias", ViewData["Agencias"]));
    vDataDictonary.Add(new KeyValuePair<string, object>("SendType", ViewData["SendType"]));
    vDataDictonary.Add(new KeyValuePair<string, object>("OrderStatus", ViewData["OrderStatus"]));
    vDataDictonary.Add(new KeyValuePair<string, object>("User", ViewData["User"]));
    vDataDictonary.Add(new KeyValuePair<string, object>("ServicesType", ViewData["ServicesType"]));


    if (Model.report == null){
        Html.RenderPartial("getReport", new ReportSchedule() { StartDate = DateTime.Now, EndDate=DateTime.Now}, vDataDictonary);
    }
    else {
        Html.RenderPartial("getReport", Model.report, vDataDictonary);
    }
}

@if (Model.lReport.Count() == 0)
 {
    <h4 class="alert alert-danger text-center">Error.</h4>
 }

else
{    
<div class="container-fluid container-fixed-lg bg-white" id="viewform">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"></div>
            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <table class="table table-hover demo-table-search" id="tableWithSearch">
              <thead>
                <tr>
                    <th></th>
                    <th></th> 
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                </tr>
              </thead>
              <tbody>
                @foreach (var item in Model.lReport)
                {
                          <tr> 
                                <td>@Html.DisplayFor(modelItem => item.ReportType.Description)</td>
                                <td>@(item.EndDate.HasValue ? String.Concat(item.StartDate.ToString("dd/MM/yyyy"), " - ", item.EndDate.Value.ToString("dd/MM/yyyy")) : item.StartDate.ToString("dd/MM/yyyy"))</td>
                                <td>@item.StartDate - @item.EndTime </td>
                                <td>@Html.DisplayFor(modelItem=> item.Agency.CorporateName) </td>
                                <td>@Html.DisplayFor(modelItem=> item.CostCenter.Description)</td>
                                <td>@Html.DisplayFor(modelItem=> item.User.Name )</td>
                                <td>@Html.DisplayFor(modelItem=>item.OrderStatus.Description )  </td>
                                <td>@Html.DisplayFor(modelItem=> item.ServiceType.Description ) </td>
                                <td>@Html.CheckBox("A", @item.Live.Equals(0) ? false : true, new {disabled="readonly" })</td>
                                <td class="v-align-middle">
                                    <div class="btn-group">
                                        <input type="button" class="btn btn-info btn-sm" id="@item.ReportScheduleID" onclick="getReport(this.id)" />                                            
                                         @Html.ActionLink(item.Live ? "D" : "A", "Live", new { id = item.CostCenterID }, new { @class ="btn btn-danger btn-sm buttonLive" })
                                    </div> 
                                </td>
                            </tr>
                }
              </tbody>
            </table>
            <div class="row">&nbsp;</div>
        </div>
     </div>
</div>
 }

我的部分观点:

@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<form id="frm" class="form-horizontal well">

@if (Model.AgencyID.Equals(0))
{
using (Html.BeginForm("getReport", "ReportSchedules"))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    @Html.HiddenFor(_ => _.ReportScheduleID)
    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <label class="control-label col-md-1">Relatório</label>
                @Html.DropDownListFor(m=>m.ReportTypeID,(SelectList) ViewData["ReportType"],"Atendimento por Agência")
                @Html.ValidationMessageFor(m=>m.ReportTypeID)
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-group">
                <label class="control-label col-md-1">Início</label>
                 <input type="text" id="date" name="date" class="col-md-2 smallDate" value="@Model.StartDate.ToShortDateString()"/>
                 @Html.ValidationMessageFor(m=>m.StartDate)
                  @Html.TextBoxFor(m=>m.StartTime,new {@class="col-md-1 smallDateMargin"})
                  @Html.ValidationMessageFor(m => m.StartTime)
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-group">
                <label class="control-label col-md-1">Periodicidade</label>
                  @Html.DropDownListFor(m=>m.SendTypeID,(SelectList) ViewData["SendType"],"Selecione")
                  @Html.ValidationMessageFor(m=>m.SendTypeID)                
            </div>
        </div>
        <div class="col-md-10">
            <h6>Configurações avançadas<a data-toggle="tooltip" class="tooltipLink" data-original-title="Data até quando o relatório será enviado.">
                    <span class="glyphicon glyphicon-question-sign"></span>
                </a></h6>
            <div class="form-group" style="border: 1px dashed #ccc;">
                <div class="form-group">
                      <label class="control-label col-md-1">   

                          <input type="checkbox" name="dateEndSelected" id="dateEndSelected" value="@Model.Live"/> Fim</label> 
                         <input type="text" id="dataFinalSelected" name="dataFinalSelected" style="display:none;" value="false"/>                               
                          <input type="text" id="dateFinal" name="dateFinal" style="display:none;"class="col-md-2 smallDate" value="@Model.EndDate.Value.ToShortDateString()"/>

                      @Html.TextBoxFor(m=>m.EndTime,new {@class="col-md-1 smallDateMargin"})
                      @Html.ValidationMessageFor(m=>m.EndTime) 
               </div>   
            </div>
        </div>
        <div class="col-md-10">
            <h6>Filtrar por:</h6>
            <div class="form-group" style="border: 1px dashed #ccc;">
                <div class="col-md-10">
                    @Html.DropDownListFor(m=>m.AgencyID,  (SelectList) ViewData["Agencias"],"Agências",new {@class="col-md-1",id="agencia",name="agencia"})
                    @Html.ValidationMessageFor(m => m.AgencyID)  

                    @Html.DropDownListFor(m=>m.OrderStatusID, (SelectList) ViewData["OrderStatus"],"Status",new {@class="col-md-2"})  
                    @Html.ValidationMessageFor(m=>m.OrderStatusID)   

                    @Html.DropDownListFor(m=>m.UserID, (SelectList) ViewData["User"],"Usuários",new {@class="col-md-3"})
                    @Html.ValidationMessageFor(m=>m.UserID)    
                 </div>
                 <div class="col-md-10">
                     @Html.DropDownList("dpdCostCenter", new List<SelectListItem>(), "Selecione", new { @class = "col-md-1" })


                     @Html.DropDownListFor(m=>m.ServiceTypeID,(SelectList) ViewData["ServicesType"],"Tipos de atendimento",new {@class="col-md-2"})
                     @Html.ValidationMessageFor(m=>m.ServiceTypeID)
                 </div>      
            </div>
        </div>
        <div class="col-md-10">
             <div class="form-group">
                <div class="input-group pull-right marginTopBot-sm input-group-sm" style="padding-left: 5px;">
                    <input type="submit" value="Agendar o envio" class="btn btn-success" onclick="$('#getResponse').submit()" />
                </div>
             </div>
        </div>
    </div>
 }
 }
  else

但如果我点击按钮提交,则转到视图的索引操作

公共ActionResult索引()

而不是:

   [HTTPPOST]
   public ActionResult getReport(ReportSchedule model)
    {
        if (ModelState.IsValid)
        {
            db.ReportSchedule.Add(model);
            db.SaveChanges();
        }
        else
        {
            return PartialView(model);
        }
        return RedirectToAction("Index");
    }

但我需要提交按钮调用getReport来验证模型并保存在db

1 个答案:

答案 0 :(得分:1)

您有嵌套的表单,这些表单是无效的HTML并且不受支持。从部分内容中删除外部表单标记(包括其上方的AntiForgeryTokenValidationSummary

<form id="frm" class="form-horizontal well"> // remove this and its corresponding closing tag