2

送信ボタンを含む複数のフォームのリストに関する問題に直面しています。最初のボタンをクリックすると正しく投稿されますが、2番目の送信ボタンをクリックすると空のコレクションが送信されます...以下は私のコードです:

インデックス.cshtml

@model MVCFormsSubmitting.Models.FormsRepository
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>
@for (int i = 0; i < Model.UserInfo.Count; i++)
{
    using (Html.BeginForm("Index", "Forms", FormMethod.Post, new {name = "Form" + @i}))
    {
         <b>@Html.EditorFor(m => m.UserInfo[i].Name)</b>
         <input name="button @i" type="submit" class="left btn btn-primary" value="Ret navne">
         <br/>
    }
}

Formsrepository.cs

namespace MVCFormsSubmitting.Models
{
  public class Info
  {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Age { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
  }

  public class FormsRepository
  {
    public FormsRepository ()
    {
      this.UserInfo = new List<Info>();
    }

    public List<Info> UserInfo { get; private set; }

    public async Task Load()
    {
      this.UserInfo.Clear();
      UserInfo = await LoadUsers();

    }

    public async static Task<List<Info>> LoadUsers()
    {
      List<Info> info = new List<Info>();

      info.Add(new Info(){
        Age = "32,",
        Email = "mail@mail.com",
        Name = "John Doe",
        Phone = "123456749",
        Id = 0

      });

      info.Add(new Info()
        {
          Age = "36",
          Email = "exmaple@example.com",
          Name = "Jane Doe",
          Phone = "987654321",
          Id = 1
        });

      return info;
    }
  }
}

FormsController.cs

public class FormsController : Controller
{
  //
  // GET: /Forms/
  public ActionResult Index()
  {
    FormsRepository.Load();

    return View(FormsRepository);
  }

  [HttpPost]
  public ActionResult Index(FormsRepository text)
  {
    return RedirectToAction("Index");
  }

  private static FormsRepository _repository;

  public static FormsRepository FormsRepository
  {
    get
    {
      if (_repository == null)
      {
        _repository = new FormsRepository();
      }

      return _repository;
    }
  }
}

Formscontroller の HttpPost アクションにブレークポイントを設定すると、最初の送信ボタンをクリックすると 1 つのアイテムが送信されますが、2 番目のボタンをクリックするとアイテムは null になります ...

助けてください :)

4

2 に答える 2

5

このコードを機能させるために、いくつかの変更を行いました。

1) コントローラー/ビューを変更して、各 UserInfo を厳密に型指定された部分ビューとしてレンダリングします。

// This method will receive an info to process, so the model binder will build the model    back in the server correctly
[HttpPost]
public ActionResult Index(Info userInfo)
{
  return RedirectToAction("Index");
}

// This method will render a partial view for a user info
[ChildActionOnly]
public ActionResult GetUserInfo(Info userInfo)
{
  return PartialView("_UserInfo", userInfo);
}

2) ビューを変更して、リポジトリ内の各ユーザー情報の部分ビューをレンダリングします。

@model MVCFormsSubmitting.Models.FormsRepository
@{
   ViewBag.Title = "Index";
   Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>
@for (int i = 0; i < Model.UserInfo.Count; i++)
{
  {Html.RenderAction("GetUserInfo", Model.UserInfo[i]);}
}

3) ユーザー情報「_UserInfo」をレンダリングする部分ビューを作成します。

@model MVCFormsSubmitting.Models.Info

@using (Html.BeginForm("Index", "Forms", FormMethod.Post, new { id = "Form" + @Model.Id, name = "Form" + @Model.Id }))
{
  <b>@Html.EditorFor(m => m.Name)</b>
  <input id="button_@Model.Id" name="button_@Model.Id" type="submit" class="left btn btn- primary" value="Ret navne">
  <br/>
}
于 2013-05-27T14:08:40.963 に答える