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

查看:236
本文介绍了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);
}

问题是,当我在任意行中按Delete按钮时,它会从列表底部删除记录,无论我单击要删除的那一行,都将从列表底部删除.

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.

推荐答案

您回发了整个集合,因此所有属性值都已添加到ModelState,并且生成了表单控件的HtmlHelper方法(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.

您可以通过添加

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天全站免登陆