2016-09-20 67 views
-1

我的MVC應用程序有問題。MVC5 Ajax.BeginForm,UpdateTargetId no render PartialView

我的控制器:

public class CustomerController : Controller 
{ 
    CustomerFacade cf = new CustomerFacade(); 

    public ActionResult Create() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public void Create(Customers customers) 
    { 
     if (customers != null) 
     { 
      cf.CreateCustomer(customers, UserSession.UserId); 
     } 

     // return View(); 
    } 


    public ActionResult GetAllCustomers() 
    { 
     var allCustomers = cf.GetAllCustomers(); 
     return PartialView("InsuranceCustomer", allCustomers); 
    } 
} 

我的主視圖:

<div class="x_content"> 

     <div id="mainb" style="height:350px;"> 

      <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-lg"> 
       <i class="fa fa-plus"></i> 
      </button> 
      @*render modal*@ 
      @Html.Partial("~/Views/Customer/CreateModal.cshtml") 

      <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-lg"> 
       <i class="fa fa-database"></i> 
      </button> 
      @*render modal*@ 

      <div id="customerId"> 
       @Html.Action("GetAllCustomers", "Customer") 
      </div> 

     </div> 
    </div> 

局部視圖CreateModal.cshtml是自舉modalpopup一種形式:

例如:

<div class="modal-body"> 
      @using (Ajax.BeginForm("Create", "Customer", null, new AjaxOptions 
      { 
       HttpMethod = "Post", 
       UpdateTargetId = "customerId", 
       OnSuccess = "$('#customerModal').modal('hide')" 
      })) 
      { 
       @Html.AntiForgeryToken() 
       <div class="form-horizontal"> 
        @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
        <div class="form-group"> 
         <div class="col-md-4"> 
          @Html.TextBoxFor(model => model.FirstName, new { @required = "require", @class = "form-control", placeholder = @Resources.InsuranceCustomer.FirstName }) 
          @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" }) 
         </div> 
         <div class="col-md-4"> 
          @Html.TextBoxFor(model => model.LastName, new { @required = "require", @class = "form-control", placeholder = @Resources.InsuranceCustomer.LastName }) 
          @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" }) 
         </div> 
        </div> 

        // and other field form 

       <div class="modal-footer"> 
        <button type="submit" class="btn btn-default" data-dismiss="modal">Close</button> 
        <input type="submit" value="@Resources.Common.Save" class="btn btn-success" /> 
       </div> 
      } 
      @*<div> 
        @Html.ActionLink("Back to List", "Index") 
       </div>*@ 
     </div> 

和InsuranceCustomer樣子:

@model IEnumerable<Insurance_System.Models.Customers> 

    <p> 
@Html.ActionLink("Create New", "Create") 
</p> 
<table class="table"> 
<tr> 

    <th> 
     @Html.DisplayNameFor(model => model.FirstName) 
    </th> 
</tr> 
@foreach (var item in Model) { 
<tr>  
    <td> 
     @Html.DisplayFor(modelItem => item.FirstName) 
    </td> 
</tr>} 

我想以後submiting模式CreateModal再次加載InsuranceCustomer新元素而無需重新加載頁面。

(對不起我的英語不好)

+0

什麼問題?你甚至沒有解釋實際發生的事情。你有沒有包含相關的腳本('jquery.unobtrusive-ajax.js') –

回答

0
控制器

public ActionResult Create() 
 
    { 
 
     return View(); 
 
    } 
 

 
    [HttpPost] 
 
    public void Create(Customers customers) 
 
    { 
 
     var cust = new customer 
 
     { 
 
     name=customers.name, 
 
     etc.... 
 
     } 
 
     db.customers.add(cust); 
 
     db.savechanges(); 
 
     var listcustomers = db.customers.ToList(); 
 
     return PartialView("InsuranceCustomer",listcustomers); 
 
    }

在查看

<div class="x_content"> 
 

 
     <div id="mainb" style="height:350px;"> 
 
      <div id="customerId"> 
 
         @*here will be load your partialview*@ 
 
      </div> 
 
     </div> 
 
    </div> 
 
<div class="modal-body"> 
 
     @using (Ajax.BeginForm("Create", "Customer", null, new AjaxOptions 
 
     { 
 
      HttpMethod = "Post", 
 
      UpdateTargetId = "customerId", 
 
      OnSuccess = "$('#customerModal').modal('hide')" 
 
     })) 
 
     { 
 
      @Html.AntiForgeryToken() 
 
      <div class="form-horizontal"> 
 
       @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
 
       <div class="form-group"> 
 
        <div class="col-md-4"> 
 
         @Html.TextBoxFor(model => model.FirstName, new { @required = "require", @class = "form-control", placeholder = @Resources.InsuranceCustomer.FirstName }) 
 
         @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" }) 
 
        </div> 
 
        <div class="col-md-4"> 
 
         @Html.TextBoxFor(model => model.LastName, new { @required = "require", @class = "form-control", placeholder = @Resources.InsuranceCustomer.LastName }) 
 
         @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" }) 
 
        </div> 
 
       </div> 
 

 
       // and other field form 
 

 
      <div class="modal-footer"> 
 
       <button type="submit" class="btn btn-default" data-dismiss="modal">Close</button> 
 
       <input type="submit" value="@Resources.Common.Save" class="btn btn-success" /> 
 
      </div> 
 
     } 
 
     @*<div> 
 
       @Html.ActionLink("Back to List", "Index") 
 
      </div>*@ 
 
    </div>

在局部視圖

@model IEnumerable<Insurance_System.Models.Customers> 
 

 
    <p> 
 
@Html.ActionLink("Create New", "Create") 
 
</p> 
 
<table class="table"> 
 
<tr> 
 

 
    <th> 
 
     @Html.DisplayNameFor(model => model.FirstName) 
 
    </th> 
 
</tr> 
 
@foreach (var item in Model) { 
 
<tr>  
 
    <td> 
 
     @Html.DisplayFor(modelItem => item.FirstName) 
 
    </td> 
 
</tr>}