如何知道在mvc中点击了哪个按钮 [英] how to know which button is clicked in 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 keyprofileList
an enumerable/list ofSelectListItem
, but you do not build such list in yourIndex
method.
2.Your variablelist
from yourIndex
method is an object of typeSelectList
.
3.Better is to use aModel
for your view and not to pass data by usingViewBag
. 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屋!