asp.net mvc 表单包括 for 循环表单控制器 [英] asp.net mvc form includes for loop over form controllers

查看:30
本文介绍了asp.net mvc 表单包括 for 循环表单控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提交一个包含 for 循环的表单,它在表单控制器上循环,因为我想将模型列表传递到 post 方法操作中,这样每个表单控制器( TextBoxFor )都由我的模型列表的对应值

i want to submit a form with for loop inside of it that loops over the form controllers since i want to pass list of model into a post method action in such a way that each and every form controller ( TextBoxFor ) is populated by corresponding value of my list of model

我的模型:

public class ExcelRowData
{
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Score { get; set; }
    public string EnrollmentTime { get; set; }
    public string GraduationTime { get; set; }
}

这是我的观点:

@model List<NewTest.Models.ExcelRowData>
@using (Html.BeginForm("Delete", "MyTest", FormMethod.Post))
{
    for (var i = 0 ; i < Model.Count ; i++)
    {
        <tr>
            <td>@Html.TextBoxFor(p => Model[i].LastName)</td>
            <td>@Html.TextBoxFor(p => Model[i].FirstName)</td>
            <td>@Html.TextBoxFor(p => Model[i].Score)</td>
            <td>@Html.TextBoxFor(p => Model[i].EnrollmentTime)</td>
            <td>@Html.TextBoxFor(p => Model[i].GraduationTime)</td>
            <td><input type="submit" formaction="@Url.Action("Delete", "MyTest", new {Ln = Model[i].LastName})" value="Delete"/></td>
        </tr>
    }
}

我从excel文件中填充了我的模型,然后我在视图中显示了上表中的excel数据,然后在删除记录的情况下有一个删除按钮

i populated my model from excel file then i show the excel data in above table in view and then there is a delete button in case of deleting a record

在我的删除操作中,我通过从提交按钮传递的参数删除选定的记录,之后我重新填充我的模型,然后再次将其传递给同一个视图

and in my delete action i delete the selected record by the parameter passed from submit button , after that i repopulate my model and again i pass it to the same view

这是我的控制器中的删除操作:

this is the delete action in my controller :

[HttpPost]
public ActionResult Delete(ExcelRowData evm, string Ln)
{
    var exceldata = new List<ExcelRowData>();

    var del = evm.FirstOrDefault(p => p.LastName == Ln);
    evm.Remove(del);

    foreach (var item in evm)
    {
        exceldata.Add(
            new ExcelRowData { LastName = item.LastName, FirstName = item.FirstName, Score = item.Score, EnrollmentTime = item.EnrollmentTime, GraduationTime = item.GraduationTime}
            );
    }

    return View("ExcelTable", exceldata);
}

问题是当我在任何一行按下删除按钮时,它会从列表底部删除记录,我点击删除哪一行并不重要,它会从列表底部删除.

the problem is when i press delete button in any row , it delets the record from bottom of the list , it doesn't matter which row i click to delete , it deletes from bottom of the list.

推荐答案

你发回了整个集合,所以所有的属性值都被添加到 ModelStateHtmlHelper 生成表单控件的方法(PasswordFor() 除外)使用来自 ModelState(如果存在)的值而不是属性值.

You posting back the whole collection, so all the property values have been added to ModelState, and the HtmlHelper methods that generate form controls (except PasswordFor()) use the values from ModelState (if they exist) rather the the property value.

你可以通过添加来解决这个问题

You can solve this by adding

ModelState.Clear();

在返回视图之前,正确的方法是遵循 PRG(发布,重定向获取)模式,并重定向到您的 GET 方法.

before you return the view, however the correct approach is to follow the PRG (Post, Redirect Get) pattern, and redirect to your GET method.

要了解 HtmlHelper 方法为何使用 ModelState,请参阅 这个答案.

To understand why the HtmlHelper methods use ModelState, refer this answer.

作为旁注,您可以通过使用 ajax 删除项目来提高性能(如果成功删除,则从 DOM 中删除相应的行),但这意味着您还需要为集合索引器包含一个隐藏的输入,以便如果您还在视图中的其他地方发回更新的集合,则可以发回非零/非连续索引器.输入将是 <input type="hidden" name="Index" value = "@i"/>

As a side note, you can improve performance by using ajax to remove the item (and if successfully deleted, remove the corresponding row from the DOM), but that means you also need to include a hidden input for the collection indexer so that you can post back non-zero/non-consecutive indexers if your also posting back the updated collection elsewhere in your view. The input would be <input type="hidden" name="Index" value = "@i" />

这篇关于asp.net mvc 表单包括 for 循环表单控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆