剃刀表删除的行保留ID

时间:2019-03-19 19:36:07

标签: c# html asp.net-mvc razor

我正在使用Razor显示模型列表,最终用户可以在该列表中添加或删除条目。单击“删除”按钮后,视图模型将被踢回到Controller Post中以删除列表条目,并通过return View(model);返回视图。但是,每个条目的隐藏ID似乎刚刚删除了最后一行,而其他显示的行又向上移动了一个,如果将其他项添加到列表中,则会导致数据重新填充为错误的ID。

控制器正在生成正确的数据,已通过检查return View(model);上的参数进行了验证,因此问题在于HTML表“保存”了一些值,而不是在每次返回后都擦除并重新创建它到视图。

换句话说,如果第一个RecipientUserID为14,第二个为20,则删除14行首先会保留显示的数据20,但是View源显示RecipientUserID为14,添加另一个项会更改第一行从20的数据到14的数据。有没有一种方法可以重新加载表,并使隐藏字段及其关联的其他数据位于同一行上?预先感谢!

查看模型:

public class NewMailModel
{
    public string Title { get; set; }

    public string Message { get; set; }

    public List<RecipientModel> Recipients { get; set; }

    public int CorporateID { get; set; }

    public string AddedName { get; set; }
}

收件人模型:

public class RecipientModel
{
    public int RecipientUserID { get; set; }

    public string RecipientName { get; set; }

    public string RecipientEmail { get; set; }

    public bool RemoveRecipient { get; set; }
}

相关剃刀:

<div class="row">
    <div class="col-lg-12">
        <table class="table table-bordered" id="cTable">
            <thead>
                <tr>
                    <th style="display:none"></th>
                    <th class="text-center">Name</th>
                    <th class="text-center">E-mail</th>
                    <th class="text-center">Select</th>
                </tr>
            </thead>
            <tbody>
                @if (Model.Recipients.Any())
                {
                    for (var i = 0; i < Model.Recipients.Count; i++)
                    {
                        <tr>
                            <td style="display:none">@Html.HiddenFor(m => m.Recipients[i].RecipientUserID)</td>
                            <td align="center">@Model.Recipients[i].RecipientName</td>
                            <td align="center">@Model.Recipients[i].RecipientEmail</td>
                            <td align="center">@Html.CheckBoxFor(m => m.Recipients[i].RemoveRecipient)</td>
                        </tr>
                    }
                }
            </tbody>
        </table>
    </div>
</div>
<div class="row">
    <div class="col-lg-9">
        <button type="submit" id="removeRecipient" class="btn btn-info removeRecipient pull-right" value="Remove Recipient" name="buttonCommand">Remove Selected</button>
    </div>
    <div class="col-lg-3">

     </div>
 </div>

控制器:

[HttpPost]
public ActionResult NewMail(NewMailModel model, string buttonCommand)
{
    if (buttonCommand == "Send")
    {
        //Miscellaneous code
    }
    else if (buttonCommand == "Add")
    {
        //After a removal and subsequent adding, the removed 
        //     RecipientUserID is used here because it was never 
        //     evacuated from the Razor table!!!
        RecipientModel added = GetFullRecipient(model.AddedName);
        int recipientIndex = -1;
        if (model.Recipients != null)
        {
            recipientIndex = model.Recipients.FindIndex(m => m.RecipientUserID == added.RecipientUserID);
            model.Recipients = GetMissingData(model.CorporateID, model.Recipients);
        }
        else
        {
            model.Recipients = new List<RecipientModel>();
        }
        if (recipientIndex == -1)
        {
            model.Recipients.Add(added);
        }
    }
    else
    {
        //Remove from list works, but the removed RecipientUserID 
        //     is still hidden in the Razor table and is assigned 
        //     to the next row that moves up into the deleted row's space!!!
        if (model.Recipients != null && model.Recipients.Count > 0)
        {
            List<RecipientModel> newRecipients = new List<RecipientModel>();
            foreach (var entry in model.Recipients)
            {
                if (entry.RemoveRecipient == false)
                {
                    RecipientModel mailTo = new RecipientModel
                    {
                        RecipientEmail = entry.RecipientEmail,
                        RecipientName = entry.RecipientName,
                        RecipientUserID = entry.RecipientUserID,
                        RemoveRecipient = false
                    };
                    newRecipients.Add(mailTo);
                }
            }
            model.Recipients = new List<RecipientModel>();
            model.Recipients = newRecipient;
            model.Recipients = GetMissingData(model.CorporateID, model.Recipients);
        }
        else
        {
            model.Recipients = new List<RecipientModel>();
        }
    }
    return View(model);
}

[HttpGet]
public List<RecipientModel> GetMissingData(int CorporateID, List<RecipientModel> Recipients)
    {
        DbEntities db = new DbEntities();
        var people = db.uspGetUsers(CorporateID).ToList();
        for (int i = 0; i < Recipients.Count; i++)
        {
            for (int x = 0; x < people.Count; x++)
            {
                if (Recipients[i].RecipientUserID == people[x].UserID)
                {
                    Recipients[i].RecipientEmail = people[x].UserEmail;
                    Recipients[i].RecipientName = people[x].UserLastName + ", " + people[x].UserFirstName;
                    Recipients[i].RemoveRecipient = false;
                    break;
                }
            }
        }

        return Recipients;
    }

具有2个表行的HTML源:

<tbody>
    <tr>
        <td style="display:none"><input data-val="true" data-val-number="The field RecipientUserID must be a number." data-val-required="The RecipientUserID field is required." id="Recipients_0__RecipientUserID" name="Recipients[0].RecipientUserID" type="hidden" value="14" /></td>
        <td align="center">Simpson, Lisa</td>
        <td align="center">email@gmail.com</td>
        <td align="center"><input data-val="true" data-val-required="The RemoveRecipient field is required." id="Recipients_0__RemoveRecipient" name="Recipients[0].RemoveRecipient" type="checkbox" value="true" /><input name="Recipients[0].RemoveRecipient" type="hidden" value="false" /></td>
    </tr>
    <tr>
        <td style="display:none"><input data-val="true" data-val-number="The field RecipientUserID must be a number." data-val-required="The RecipientUserID field is required." id="Recipients_1__RecipientUserID" name="Recipients[1].RecipientUserID" type="hidden" value="25" /></td>
        <td align="center">Scorpio, Hank</td>
        <td align="center">email@gmail.com</td>
        <td align="center"><input data-val="true" data-val-required="The RemoveRecipient field is required." id="Recipients_1__RemoveRecipient" name="Recipients[1].RemoveRecipient" type="checkbox" value="true" /><input name="Recipients[1].RemoveRecipient" type="hidden" value="false" /></td>
    </tr>
</tbody>

在删除第一行之后有1个表行的HTML源代码(请注意RecipientUserID是已删除行的值(14),而不是正在显示的行/数据的值(25)!)

<tbody>
    <tr>
        <td style="display:none"><input data-val="true" data-val-number="The field RecipientUserID must be a number." data-val-required="The RecipientUserID field is required." id="Recipients_0__RecipientUserID" name="Recipients[0].RecipientUserID" type="hidden" value="14" /></td>
        <td align="center">Scorpio, Hank</td>
        <td align="center">email@gmail.com</td>
        <td align="center"><input checked="checked" data-val="true" data-val-required="The RemoveRecipient field is required." id="Recipients_0__RemoveRecipient" name="Recipients[0].RemoveRecipient" type="checkbox" value="true" /><input name="Recipients[0].RemoveRecipient" type="hidden" value="false" /></td>
    </tr>
</tbody>

0 个答案:

没有答案