English 中文(简体)
获取以“Max”作为id名称结尾的所有表单元素
原标题:get all form elements that end with "Max" as the id name

我基本上有几个输入字段,它们共享相同的最后3个字符“Max”。它们都不同于它们所属的“div”标记。我现在拥有的代码块看起来很糟糕:

                    if (Convert.ToInt32(Request.Form["MCQMax"]) > 0)
                {
                    TempData["Max"] = Convert.ToInt32(Request.Form["MCQMax"]);
                }
                else if (Convert.ToInt32(Request.Form["SAMax"]) > 0)
                {
                    TempData["Max"] = Convert.ToInt32(Request.Form["SAMax"]);
                }
                else if (Convert.ToInt32(Request.Form["PLMCQMax"]) > 0)
                {
                    TempData["Max"] = Convert.ToInt32(Request.Form["PLMCQMax"]);
                }
                else if (Convert.ToInt32(Request.Form["PLIFMax"]) > 0)
                {
                    TempData["Max"] = Convert.ToInt32(Request.Form["PLIFMax"]);
                }
                else if (Convert.ToInt32(Request.Form["PLDMax"]) > 0)
                {
                    TempData["Max"] = Convert.ToInt32(Request.Form["PLDMax"]);
                }

我如何传递以Max结尾的任何形式的元素,而不是有这个冗余的代码块?谢谢大家!!

最佳回答

类似于以下内容:

var firstMaxFormKey = Request.Form.AllKeys
     .Where(arg => arg.EndsWith("Max"))
     .FirstOrDefault();
TempData["Max"] = Convert.ToInt32(Request.Form[firstMaxFormKey]);

[编辑]

很抱歉没有引起足够的注意。我认为这正是您所需要的:

var max = Request.Form.AllKeys
        .Where(arg => arg.EndsWith("Max"))
        .Select(arg => Convert.ToInt32(Request.Form[arg]))
        .Where(arg => arg > 0)
        .FirstOrDefault();
问题回答

您可以将所有ID放入一个数组中,然后循环遍历它们:

(未经测试的半伪代码)

IDs={"MCAMax",...}

for(int i=0; i<IDs.length; i++){
   int theMax=Convert.ToInt32(Request.Form[IDs[i]]);
   if ( theMax > 0){
      TempData["Max"] = theMax;
      break;
   }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签