将多个值传递给 ActionMethodSelectorAttribute

Pass Multiple values to ActionMethodSelectorAttribute

我正在使用 ASP.Net 4.61,MVC5

我想在同一操作中捕获来自 3 个按钮的提交。

我正在使用 ActionMethodSelectorAttribute 来捕捉按下了哪个提交按钮。一个提交按钮工作正常。但是如下所示,如果我想在一个action方法中捕获3个按钮,它不起作用,我不知道如何将List传递给属性。

单击 Eng、Mat 或 ICT 时,我想显示已单击按钮的成绩列表。这 3 个按钮的性质几乎相同。他们进入数据库并根据 Subject

提取数据

FromValueRequiredAttribute.cs

public class FromValueRequiredAttribute : ActionMethodSelectorAttribute
    {
        public string ButtonName { get; set; }
        public List<string> ButtonNameList { get; set; }

        public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
        {
            return controllerContext.Controller.ValueProvider.GetValue(ButtonName) != null;
        }
    }

所以,目前,我为这 3 个按钮指定了相同的名称,如下所示,并检查按钮的值以检测单击了哪个按钮。

<button type="submit" name="ViewSubject" class="btn btn-success" value="ENG">English</button>
<button type="submit" name="ViewSubject" class="btn btn-info" value="MAT">Maths</button>
<button type="submit" name="ViewSubject" class="btn btn-warning" value="ICT">ICT</button>

你能告诉我如何将列表传递给属性吗?

Updated1 : 这就是我目前正在做的事情。

When Eng, Mat or ICT is clicked, I would like to display list of grades for the Clicked button. The nature of these 3 buttons are almost the same.

您只需在 HttpPost 操作方法中使用与提交按钮名称同名的参数即可。

[HttpPost]
public ActionResult Index(string ViewSubject)
{
    var g = GradeService.GetExamGradesToUpdate(ViewSubject);
    // do something with g
    // to do : Return something
}

当这些按钮之一(在表单中)被点击时,参数 ViewSubject 将具有被点击按钮的值(value 属性值)。