我将对象列表传递给视图,并允许用户更新单个对象的属性。
这是get动作。我在此处将视图列表传递给视图。
public ActionResult EditArticleList(int id)
{
ArticleList articleList = unitOfWork.ArticleListRepository.GetById(id);
IEnumerable<Article> Articles = articleList.Articles;
return View(Articles);
}
这是观点。
@model IEnumerable<Domain.Entities.Article>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>EditArticleList</title>
</head>
<body>
@for (int i = 0; i < Model.Count(); i++)
{
using (Html.BeginForm("EditArticleList", "AdminArticleList", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.EditorFor(model => Model.ToList()[i].Title, null, "Title")
@Html.HiddenFor(model => Model.ToList()[i].Id)
<input type="submit" />
<br />
}
}
这是后期行动。
public ActionResult EditArticleList(Article art)
{
}
我通过3篇文章,ID为1,2,3。 但无论我更新和发布哪篇文章,id都会返回1。 标题正确返回。
答案 0 :(得分:0)
这比仅使用mvc的解决方案更有效,这需要每个按钮都有一个id。如果你想让我研究一个querystring pure mvc解决方案,你能告诉我吗?:
查看:
@model IEnumerable<Testy20161006.Controllers.Article>
@using Testy20161006.Controllers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>EditArticleList</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
function SubmitTheForm(theId) {
$("#theId").val(theId);
$("#formName").submit();
}
</script>
</head>
<body>
@Html.AntiForgeryToken()
@*USE YOUR own controller below*@
@using (Html.BeginForm("EditArticleList", "Home", FormMethod.Post, new { id = "formName" }))
{
<input type="hidden" id="theId" name="theId" />
<table>
@foreach (Article item in Model)
{
<tr>
<td></td>
<td>
@Html.EditorFor(model => item.Title, null, "Title")
</td>
<td>
<input type="button" onclick="SubmitTheForm('@item.Id')" value="Select" />
</td>
</tr>
}
</table>
<br />
}
</body>
</html>
控制器/视图模型:
//your code might be slightly different, but if you look at the view you can solve your issue
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
}
public class ArticleList
{
public IList<Article> Articles { get; set; }
}
namespace unitOfWork
{
public static class ArticleListRepository
{
public static ArticleList GetById(int id)
{
var article1 = new Article { Id = 1, Title = "article1" };
var article2 = new Article { Id = 2, Title = "article2" };
var article3 = new Article { Id = 3, Title = "article3" };
ArticleList articleList = new ArticleList();
articleList.Articles = new List<Article>();
articleList.Articles.Add(article1);
articleList.Articles.Add(article2);
articleList.Articles.Add(article3);
return articleList;
}
}
}
public class HomeController : Controller
{
[HttpPost]
public ActionResult EditArticleList(int theId, Article art)
{
//put breakpoint here to see the id returned.
return View(art);
}
public ActionResult EditArticleList(int? id)
{
ArticleList articleList = unitOfWork.ArticleListRepository.GetById(5); //hard coding for demo
IEnumerable<Article> Articles = articleList.Articles;
return View(Articles);
}