3

テキスト ボックスのグループを含むフォームを作成したいのですが、ユーザーが追加ボタンをクリックするたびに、ユーザーが追加ボタンをクリックするたびにそれらのテキスト ボックスが再作成されます。これが私がやろうとしていることの写真です。ここに画像の説明を入力

コントローラ:

    //
    // 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);
    }

作成.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>

ビューモデル

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; }
}
}

部分図

@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>
4

3 に答える 3

6

次の記事を読むことを強くお勧めします。探しているものを達成する方法の例が含まれています。これを実装し始めるときに、既定のモデル バインダーで発生する課題について説明します。コレクション インデックスでこれらの課題を克服するために、作成者はカスタムHtml.BeginCollectionItemヘルパーを使用します。

于 2012-07-08T06:31:24.277 に答える
2

さて、必要なモデルを使用して部分ビューを作成しようとします。たとえば、ajax を使用してボタンをクリックするたびに、部分ビューから生成された html を追加することが重要です。

あなたのモデル

 public class example
 { 
     public int Length { get; set;}
     public int Width  { get; set;}
     public int Height {get; set;}
 }

あなたの行動

public ActionResult Example()
{
     return View();
}

あなたの部分的な見方

@model FooExample.Model.Example    

@{
     Layout = null;
 }

<div>
     @Html.EditorFor(model => model.Length)  
</div>
<div>
     @Html.EditorFor(model => model.Width)  
</div>
<div>
     @Html.EditorFor(model => model.Height)  
</div>

あなたの主な見解

<input type="button" id="btnAddRows" />

<table id="addViews">
     <tr>
        <td>
        </td> 
     </tr>
<table>

これがスクリプトです

$(document).ready(function(){
   $("#btnAddRows").click(function(){

       $.ajax({
            url: 'your path to the action',
            data : 'if you need to pass parameters',
            datatype: 'html',
            success: function(data){
               $("#addViews").append("<tr/><td>"+data+"</td><tr>");
           }  
       })

   });

});
于 2012-07-08T01:24:54.180 に答える
1

過去に同様の状況をどのように実装したかについての説明は次のとおりです: https ://stackoverflow.com/a/10583792/1373170

編集と削除もサポートする必要があるため、より複雑です。主なアイデアは、EditorTemplate新しい行の作成に使用される非表示のテンプレート(で作成)と、MVCのデフォルトのバインダーによって自動的に解釈され、適切なものに変換される方法ですべての新しいアイテムに自動的に名前を付けるJavaScriptを少し用意することです。アクション内のViewModelのリスト。

于 2012-07-08T01:43:46.383 に答える