I would like to create a form with a group of text fields, and each time the user clicks the add button, these text fields will be recreated as many times as the user clicks the add button. Here is an image of what I'm looking for.
Controller:
public ActionResult Create()
{
return View();
}
[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>
source
share