我希望创建一个表单,其中包含一组文本框。每当用户点击添加按钮时,这些文本框将根据用户的点击次数重建。以下是我想要实现的图片:
控制器:
//
// 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";
}
<h1>Post a Move</h1>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<div class="form-item-group last">
<div class="form-item half">
<div class="editor-label">
Start Date
</div>
Editorfor for Model1...
<div>
@Html.Partial("_MoveItem")
</div>
</fieldset>
<div class="submit-button-wrapper">
<input class="button" type="submit" value="Post" />
</div>
}
<div>
@Html.ActionLink("Go Back", "Index", null, new { @class = "link-text" })
</div>
ViewModel
namespace MovinMyStuff.WebUI.Areas.Client.Models
{
public class CreateMoveViewModel
{
public CreateMoveViewModel()
{
Moves = new Move();
MoveItems = new MoveItem();
}
public Move Moves { get; set; }
public MoveItem MoveItems { get; set; }
}
}
Partial View
@model MovinMyStuff.Domain.Entities.MoveItem
<div class="editor-label">
Choose Area of Your Home
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MoveItemArea)
@Html.ValidationMessageFor(model => model.MoveItemArea)
</div>
<div class="editor-label">
Choose Your Item
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MoveItemType)
@Html.ValidationMessageFor(model => model.MoveItemType)
</div>
<div class="editor-label">
Quantity
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Quantity)
@Html.ValidationMessageFor(model => model.Quantity)
</div>
Other Properties of model...
<div class="editor-label">
@Html.HiddenFor(model => model.MoveId)
</div>