2015-09-24 48 views
2

我在創建表單時使用ajax.beginform當我在數據庫中創建多個條目時它創建了3個條目第二提交6行等..Ajax.beginform在提交時在asp.net中創建多個數據庫中的條目mvc

這裏是我的索引視圖:

@model IEnumerable<CourseSelection.Models.country> 
@{ 
    Layout = Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/parameterview.cshtml"; 
} 

@Scripts.Render("~/bundles/jquery") 
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js") 
<script src="~/js/main/jquery.validate.js"></script> 


<div class="btn-danger ma ci" style="height:50px ;"> 
    <h2 class="white" style=" float:left; margin-left:5px ; margin-top:2px; margin-bottom:10px;"> COUNTRIES </h2> 
    <button id="sh" class="btn btn-primary" style="float:right; margin-right:5px;"> 
     @Ajax.ActionLink(
      "Create", 
      "Create", 
      "countries", 
      new AjaxOptions 
      { 
       UpdateTargetId = "EditDivId", 
       InsertionMode = InsertionMode.ReplaceWith 

      }) 
    </button> 
</div> 



<div id="EditDivId"> 
</div> 
<div id="indexdiv"> 
<div class="nicdark_space10"></div> 
@{ Html.RenderAction("_i"); } 

</div> 

這裏是我創造的觀點:

@model CourseSelection.Models.country 

@Scripts.Render("~/bundles/jquery") 
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js") 

@using (Ajax.BeginForm("Create", "countries", FormMethod.Post, new   AjaxOptions 
{ 
UpdateTargetId = "indexdiv", 
InsertionMode = InsertionMode.ReplaceWith 


})) 
{ 
@Html.AntiForgeryToken() 

<div id="tt"> 
    <div class="nicdark_bg_blue blueb" style="margin-top:10px;"> 
     <div class="row"> 
      <div class="col-md-6"> 
       <div class="form-group"> 
        <label for="addres">Country Name</label> 
        <br /> 
        <input type="text" placeholder="countryName"  class="form-control add" id="countryName" name="countryName" /> 
       </div> 

      </div> 

      <div class="col-md-1"> 
       <div class="form-group"> 
        <label for="active">Passive</label> 
        <br /> 
        <input type="checkbox" class="form-control" value="true" id="active" name="active" style="width:34px" /> 
        <input type="hidden" value="false" name="active" /> 
       </div> 

      </div> 



      <div class="form-group"> 
       <div class="col-md-1"> 
        <label for="s"></label> 
        <br /> 
        <input type="submit" value="Create" class="btn btn-primary" id="s" style="margin-top:6px; width:80px; height:35px;" /> 
       </div> 
       <div class="col-md-1"> 
        <label for="hi"></label> 
        <input type="reset" value="Cancel" class="btn btn-danger" id="hi" style="margin-top:6px; margin-left:3px; width:80px; height:35px;" /> 
       </div> 
      </div> 
     </div> 
    </div> 

</div> 

} 

,這裏是我的控制器:

public class countriesController : Controller 
{ 
    private CourseSelectionEntities3 db = new CourseSelectionEntities3(); 

    // GET: countries 
    public ActionResult Index() 
    { 
     return View(db.countries.ToList()); 
    } 

    public PartialViewResult _i() 
    { 
     return PartialView("_i", db.countries.ToList()); 
    } 

    // GET: countries/Create 
    public PartialViewResult Create() 
    { 
     return PartialView("_c"); 
    } 

    // POST: countries/Create 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "Id,countryName,active")] country country) 
    { 
     if (ModelState.IsValid) 
     { 
      country.countryName = country.countryName.ToUpper(); 
      db.countries.Add(country); 
      db.SaveChanges(); 
      return View("Index", db.countries.ToList()); 
     } 

     return PartialView(country); 
    } 

} 
+0

你檢查了答案嗎? –

+0

@RezaAghaei謝謝它終於工作了,真的很感謝 – John

回答

2

這是因爲@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")不止一個。如果這些腳本已在佈局頁面(母版頁)中呈現,請從創建的分部視圖中甚至從其他視圖中刪除腳本部分。

重要提示:

如果這些腳本已經在你的頁面佈局(母版頁),那麼你並不需要這些腳本其他地方被渲染。

說明:

這裏是代碼的一部分,當你把jquery.unobtrusive-ajax.js在你的網頁,將運行:

$(function(){ 
    //... 
    $(document).on("submit", "form[data-ajax=true]", function (evt) { 
     var clickInfo = $(this).data(data_click) || [], 
      clickTarget = $(this).data(data_target), 
      isCancel = clickTarget && clickTarget.hasClass("cancel"); 
     evt.preventDefault(); 
     if (!isCancel && !validate(this)) { 
      return; 
     } 
     asyncRequest(this, { 
      url: this.action, 
      type: this.method || "GET", 
      data: clickInfo.concat($(this).serializeArray()) 
     }); 
    }); 
    //... 
}); 

所以每次渲染文件,此事件處理程序將附加到您的提交按鈕。

+0

謝謝你現在它是完美的工作 – John

+0

@John歡迎:) –

+0

是的,我做的感謝你:D – John

相關問題