gpt4 book ai didi

asp.net-mvc-4 - mvc4页面上的多个表单,提交一个表单

转载 作者:行者123 更新时间:2023-12-04 04:55:45 25 4
gpt4 key购买 nike

我在列出多个表单时遇到问题,包括提交按钮。
单击第一个按钮时它会正确发布,但是当我单击第二个提交按钮时,它会提交一个空集合...
下面是我的代码:

索引.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;
}
}
}

表单 Controller .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 个项目,但单击第二个按钮时,该项目为空...

请帮忙:)

最佳答案

我做了一些更改以使此代码正常工作!

1) 将 Controller / View 更改为将每个 UserInfo 渲染为强类型的局部 View :

// 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) 更改 View 以呈现存储库中每个用户信息的部分 View :
@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)创建一个局部 View 来呈现用户信息“_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/>
}

关于asp.net-mvc-4 - mvc4页面上的多个表单,提交一个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16774568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com