英文:
Displaying, Editing, and Posting a List ASP.NET Core
问题
以下是您要翻译的内容:
"I was trying to follow this question, https://stackoverflow.com/questions/9306246/posting-to-a-listmodeltype-mvc3, but I was running into a roadblock.
I want to display the full contents of a table, allow editing to that table, then submit it back to controller to save edits.
Specifically, the error I'm getting here is on Model.ClonePosts.Count
:
'IEnumerable
Aside from that immediate problem, I'm not sure that have the binding correct. Will changes made in the View get posted correctly?
Thank you in advance
Model:
public class ClonePost
{
[Key]
public int id { get; set; }
[Required]
public string PostNumber { get; set; }
[Required]
public string Type { get; set; }
[Required]
public int ItemNumber { get; set; }
public string ItemData { get; set; }
public int ItemSelected { get; set; }
}
View model:
public class ReviewCloneVM
{
public List
}
View:
@model IEnumerable<AURA.ViewModels.ReviewCloneVM>
ReviewClone
@Html.EditorFor(x => x.ClonePosts[i].PostNumber) | @Html.EditorFor(x => x.ClonePosts[i].ItemNumber) | @Html.EditorFor(x => x.ClonePosts[i].ItemData) | @Html.EditorFor(x => x.ClonePosts[i].ItemSelected) |
Controller:
public async Task
{
...
// now call up the full table for review and edit
var cloned = _context.ClonePosts.OrderByDescending(m => m.PostNumber);
return View(await cloned.ToListAsync());
}
[HttpPost]
public async Task
{
try
{
// other stuff
return RedirectToAction("PostDetail", "Post", new { zero = "230323-0" });
}
catch
{
return View(reviewCloneVM);
}
}"
英文:
I was trying to follow this question, https://stackoverflow.com/questions/9306246/posting-to-a-listmodeltype-mvc3, but I was running into a roadblock.
I want to display the full contents of a table, allow editing to that table, then submit it back to controller to save edits.
Specifically, the error I'm getting here is on Model.ClonePosts.Count
:
> 'IEnumerable<ReviewCloneVM>' does not contain a definition for 'ClonePosts' and no accessible extension method 'ClonePosts' accepting a first argument of type 'IEnumerable<ReviewCloneVM>' could be found
Aside from that immediate problem, I'm not sure that have the binding correct. Will changes made in the View get posted correctly?
Thank you in advance
Model:
public class ClonePost
{
[Key]
public int id { get; set; }
[Required]
public string PostNumber { get; set; }
[Required]
public string Type { get; set; }
[Required]
public int ItemNumber { get; set; }
public string ItemData { get; set; }
public int ItemSelected { get; set; }
}
View model:
public class ReviewCloneVM
{
public List<ClonePost> ClonePosts { get; set; }
}
View:
@model IEnumerable<AURA.ViewModels.ReviewCloneVM>
<h1>ReviewClone</h1>
<table class="table">
@*foreach (var item in Model)*@
@for (int i = 0; i < Model.ClonePosts.Count; i++) {
<tr>
<td>
@Html.EditorFor(x => x.ClonePosts[i].PostNumber)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemNumber)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemData)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemSelected)
</td>
<td>
<input type="submit" value="Clone" />
</td>
</tr>
}
</tbody>
</table>
Controller:
public async Task<IActionResult>ReviewClone(string zero = "230323-0")
{
...
// now call up the full table for review and edit
var cloned = _context.ClonePosts.OrderByDescending(m => m.PostNumber);
return View(await cloned.ToListAsync());
}
[HttpPost]
public async Task<IActionResult>ReviewClone(ReviewCloneVM reviewCloneVM)
{
try
{
// other stuff
return RedirectToAction("PostDetail", "Post", new { zero = "230323-0" });
}
catch
{
return View(reviewCloneVM);
}
}
答案1
得分: 1
你需要将你的Model
类型从@model IEnumerable<AURA.ViewModels.ReviewCloneVM>
更改为 AURA.ViewModels.ReviewCloneVM
,然后从Model中访问ClonePosts
:
@model AURA.ViewModels.ReviewCloneVM
<h1>ReviewClone</h1>
<table class="table">
@if(Model != null)
{
@*foreach (var item in Model)*@
for (int i = 0; i < Model.ClonePosts.Count; i++)
{
<tr>
<td>
@Html.EditorFor(x => x.ClonePosts[i].PostNumber)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemNumber)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemData)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemSelected)
</td>
<td>
<input type="submit" value="Clone" />
</td>
</tr>
}
}
</tbody>
</table>
你的Controller
方法将是:
public async Task<IActionResult> ReviewClone(string zero = "230323-0")
{
// ...
ReviewCloneVM model = new ReviewCloneVM();
// 现在调用完整的表以供审查和编辑
var cloned = _context.ClonePosts.OrderByDescending(m => m.PostNumber);
model.ClonePosts = await cloned.ToListAsync();
return View(model);
}
英文:
You need to change your Model
type from @model IEnumerable<AURA.ViewModels.ReviewCloneVM>
to AURA.ViewModels.ReviewCloneVM
and then access the ClonePosts
from the Model :
@model AURA.ViewModels.ReviewCloneVM
<h1>ReviewClone</h1>
<table class="table">
@if(Model !=null)
{
@*foreach (var item in Model)*@
for (int i = 0; i < Model.ClonePosts.Count; i++)
{
<tr>
<td>
@Html.EditorFor(x => x.ClonePosts[i].PostNumber)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemNumber)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemData)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemSelected)
</td>
<td>
<input type="submit" value="Clone" />
</td>
</tr>
}
}
</tbody>
</table>
Your Controller
method will be:
public async Task<IActionResult>ReviewClone(string zero = "230323-0")
{
...
ReviewCloneVM model=new ReviewCloneVM();
// now call up the full table for review and edit
var cloned = _context.ClonePosts.OrderByDescending(m => m.PostNumber);
model.ClonePosts =await cloned.ToListAsync();
return View(model);
}
答案2
得分: 0
在你的视图中,你需要使用以下代码部分作为模型:
@model AURA.ViewModels.ReviewCloneVM
所以尝试修改你的代码如下:
Controller:
public async Task<IActionResult> ReviewClone(string zero = "230323-0")
{
// 现在调用完整的表格进行审查和编辑
var cloned = _context.ClonePosts.OrderByDescending(m => m.PostNumber).ToList();
ReviewCloneVM vm = new ReviewCloneVM()
{
ClonePosts = cloned,
};
return View(vm);
}
View:
@model AURA.ViewModels.ReviewCloneVM
<h1>ReviewClone</h1>
<form method="post">
@for (int i = 0; i < Model.ClonePosts.Count; i++)
{
<tr>
<td>
@Html.EditorFor(x => x.ClonePosts[i].PostNumber)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemNumber)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemData)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemSelected)
</td>
</tr>
<br />
}
<td>
<input type="submit" value="Save" />
</td>
</form>
演示:
[![enter image description here][1]][1]
<details>
<summary>英文:</summary>
In your View, You need to use
@model AURA.ViewModels.ReviewCloneVM
as the model, So try to change your code like:
**Controller**
public async Task<IActionResult> ReviewClone(string zero = "230323-0")
{
//now call up the full table for review and edit
var cloned = _context.ClonePosts.OrderByDescending(m => m.PostNumber).ToList();
ReviewCloneVM vm = new ReviewCloneVM()
{
ClonePosts = cloned,
};
return View(vm);
}
**View**
@model AURA.ViewModels.ReviewCloneVM
<h1>ReviewClone</h1>
<form method="post">
@*foreach (var item in Model)*@
@for (int i = 0; i < Model.ClonePosts.Count; i++)
{
<tr>
<td>
@Html.EditorFor(x => x.ClonePosts[i].PostNumber)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemNumber)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemData)
</td>
<td>
@Html.EditorFor(x => x.ClonePosts[i].ItemSelected)
</td>
</tr>
<br />
}
<td>
<input type="submit" value="Save" />
</td>
</form>
**Demo**:
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/j5azD.gif
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论