在MVC3 Razor创建视图中动态添加表单元素
我想创建一个包含一组文本框的表单,每次用户单击添加按钮时,这些文本框将在用户单击添加按钮时重新创建。 这是我要做的事情的照片。
控制器:
// // GET: /Client/MyMove/Create public ActionResult Create() { return View(); } // // POST: /Client/MyMove/Create [HttpPost] public ActionResult Create(Move move) { var viewModel = new CreateMoveViewModel(); MembershipUser currentUser = Membership.GetUser(); Guid currentUserId = (Guid)currentUser.ProviderUserKey; if (ModelState.IsValid) { move.UserId = currentUserId; db.Moves.Add(move); move.AddMoveItem(2); db.SaveChanges(); return RedirectToAction("Index"); } return View(move); }
Create.cshtml
@model MovinMyStuff.WebUI.Areas.Client.Models.CreateMoveViewModel @using Telerik.Web.Mvc.UI @{ ViewBag.Title = "Create"; } Post a Move
@using (Html.BeginForm()) { @Html.ValidationSummary(true)