加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – asp.net mvc – 当按钮名称全部相同时,如何准确

发布时间:2020-12-16 07:21:26 所属栏目:asp.Net 来源:网络整理
导读:我的aspx文件中有以下代码: % using (Html.BeginForm()) { int i = 0; % % foreach (var item in Model.Educations) { % fieldset input type="hidden" name="educations.Index" value="" / p label for="PID" PID:/label %= Html.TextBox("educations["+i+
我的aspx文件中有以下代码:

<% using (Html.BeginForm())
       {

           int i = 0; 
    %>

    <% foreach (var item in Model.Educations)
       { %>
    <fieldset>
        <input type="hidden" name="educations.Index" value="" />
        <p>
            <label for="PID">
                PID:</label>
            <%= Html.TextBox("educations["+i+"].PID",item.PID)%>
            <%= Html.ValidationMessage("PID","*")%>
        </p>
        <p>
            <label for="EducationType">
                EducationType:</label>
            <%= Html.TextBox("educations["+i+"].EducationType",item.EducationType)%>
            <%= Html.ValidationMessage("EducationType","*")%>
        </p>
        <p>
            <label for="SchoolName">
                SchoolName:</label>
            <%= Html.TextBox("educations["+i+"].SchoolName",item.SchoolName)%>
            <%= Html.ValidationMessage("SchoolName","*")%>
        </p>
        <p>
            <label for="UniversityId">
                UniversityId:</label>
            <%= Html.TextBox("educations["+i+"].UniversityId",item.UniversityId)%>
            <%= Html.ValidationMessage("UniversityId","*")%>
        </p>
        <p>
            <label for="Department">
                Department:</label>
            <%= Html.TextBox("educations["+i+"].Department",item.Department)%>
            <%= Html.ValidationMessage("Department","*")%>
        </p>
        <p>
            <label for="Degree">
                Degree:</label>
            <%= Html.TextBox("educations["+i+"].Degree",String.Format("{0:F}",item.Degree))%>
            <%= Html.ValidationMessage("Degree","*")%>
        </p>
        <p>
            <label for="YearOfGraduation">
                YearOfGraduation:</label>
            <%= Html.TextBox("educations[" + i + "].YearOfGraduation",item.YearOfGraduation))%>
            <%= Html.ValidationMessage("YearOfGraduation","*")%>
        </p>
        <p>
            <label for="ID">
                ID:</label>
            <%= Html.TextBox("educations[" + i + "].ID",item.ID)%>
            <%= Html.ValidationMessage("ID","*")%>
        </p>
        <input type="submit" name="silButton" value="Sil"/>
    </fieldset>
    <% 
        i++;
       } %>
     <p>
        <input type="submit" name="ekleButton" value="Ekle" />
     </p>
    <% } %>
    <div>
        <%=Html.ActionLink("Back to List","Index") %>
    </div>

这样,如果用户想要输入其他教育信息(最有可能来自其他大学或其他学位),我可以动态添加(“Ekle”)更多字段.

此代码还提供“Sil”按钮(这意味着删除),我希望能够准确检测到哪个“Sil”按钮被按下,并从会话中的“对象”中删除该教育条目.

我的动作方法如下所示:

public ActionResult Step3(string ekleButton,IList<Education> educations,IList<string> silButton)
        {
            if (educations != null)
            {
                _person.Educations.Clear();
                _person.Educations.AddRange(educations);
            }

            if (ekleButton != null)
            {
                _person.Educations.Add(new Education());
            }

             if (silButton!=null){
              //find index and delete it from _person.Edications

           }
            return View(_person);
        }

这样,如果按下任何“silButton”按钮,silButton!= null,我无法检测到按下了哪个按钮.有没有办法找到这个?

解决方法

您可以将索引放在按钮的名称中:

<input type="submit" name="silButton<%=i %>" value="Sil" />

在你的行动方法:

var silButton = Request.Params.AllKeys.FirstOrDefault(key => key.StartsWith("silButton"));
if (!string.IsNullOrEmpty(silButton))
{
    var index = int.Parse(silButton.Replace("silButton",string.Empty));
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读