如何知道在mvc中点击了哪个按钮 [英] how to know which button is clicked in mvc

查看:139
本文介绍了如何知道在mvc中点击了哪个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查看,其中包含2个面板, SignIn SignUp .In signIn 面板我有登录按钮清除按钮,在 signUp 面板我有 SignUp 清除按钮。

当我尝试使用错误的密码登录时出现此错误。



型号。

I have a View with 2 panels which is SignIn and SignUp .In signIn Panel i have Login button and Clear button ,In signUp Panel I have SignUp and Clear Button.
I got this error when i try to login with wrong password.

Model.

public ActionResult Index()
        {

            var list = new SelectList(new[]
{
    new { ID = "1", Name = "MySelf" },
    new { ID = "2", Name = "My Son" },
    new { ID = "3", Name = "My Daughter" },
    new{ ID="4" ,Name="My Brother"},
     new { ID = "5", Name = "My Sister" },
      new { ID = "6", Name = "My Relative" },
      new { ID = "6", Name = "My Friend" },
},
"ID", "Name",1);


            ViewBag.profileList = list;

            ViewBag.ReligionList = new SelectList(db.Religion, "ReligionId", "ReligionName");

            ViewBag.languageList = new SelectList(db.Languages, "LanguageId", "LanguageName");
            ViewBag.StateList = new SelectList(db.States, "stateId", "Statename");
            return View();
        }
 [HttpPost]
        public ActionResult Index(MemberLogin memberlogin)
        {
           

                if (ModelState.IsValid)
                {

                    bool value = db.MemberLogins.Any(o => o.EmailAddress == memberlogin.EmailAddress &&
                        o.Password == memberlogin.Password);

                    if (value)
                    {
                        return RedirectToAction("Index", "MemberHome");
                    }
                    else
                    {
                        return View();
                    }
                }
               
           

            return View();
        }



我的问题是



1)[HttpPost]方法点击时点击在我的视图中的任何按钮,我如何知道哪个按钮被点击?如果我点击登录按钮我必须移动主页如果清除点击我必须清除所有控件。



2)我收到此错误




My question is

1) [HttpPost] method get fires when i click any button in my View, How do i know which button is Clicked?if i clicked signin button i got to move Home page if clear clicked i got to clear all controls.

2) I got this error

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'profileList'



当我点击 SignIn 按钮或 SignUp 按钮,但 ProfileList 在点击任何按钮之前完全加载。

我如何解决这个问题?


when i click SignIn Button or SignUp button but the ProfileList Loaded completely before i Click any button.
How do i solve this?

推荐答案

1.问题是由你的视图中你想要的密钥 profileList 枚举可用/列表 SelectListItem ,但您不在索引方法中构建此类列表。



2.您的索引中的变量 list 是一个类型的对象 SelectList



3.Better是使用模型用于您的视图,而不是使用 ViewBag 传递数据。因此,您必须创建一个具有4个属性的类,每个列表的一个属性。然后创建此类的对象并使用其项填充每个属性。
1.The problem is generated by the fact that in your view you are expecting to have for the key profileList an enumerable/list of SelectListItem , but you do not build such list in your Index method.

2.Your variable list from your Index method is an object of type SelectList.

3.Better is to use a Model for your view and not to pass data by using ViewBag. So you have to create a class that will have 4 properties, one property for each list. Then to create an object of this class and populate each property with their items.


这篇关于如何知道在mvc中点击了哪个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆