我在創建表單時使用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);
}
}
你檢查了答案嗎? –
@RezaAghaei謝謝它終於工作了,真的很感謝 – John