一般使用Ajax.BeginForm會搭配Submit button觸發,
要使用多重按鈕
1.必須要指定所有按鈕都有相同的name屬性(name="btn")
2.每個按鈕賦予不同的值(value = "Create" or value = "Update" )
3.在Controller中,除了model的參數外(ModelDto XDto),
還要加上從View傳到Control 的值,參數的命名必須跟name屬性相同(string btn )
4.利用btn當作條件做判斷
--View-----------------------------------------------------------------------------------------------------
@using (Ajax.BeginForm("Create", "DIM", new AjaxOptions { HttpMethod = "POST", Confirm = "確定要變更?", UpdateTargetId = "ajaxBody" })) { @Html.EditorFor(model => model.id)--Controller------------------------------------------------------------------------------------------------}
// POST: /DIM/Create
[HttpPost] public ActionResult Create(ModelDto XDto, string btn) { if (ModelState.IsValid) { if (btn == "Create") { db.ModelDto .Add(XDto); db.SaveChanges(); } else { db.Entry(XDto).State = EntityState.Modified; db.SaveChanges(); } } return RedirectToAction("List"); }