Friday, January 24, 2014

How to manage multiple button on one page in mvc?

Model(Login.cs)




public class Login

{


[Required]

[Display(Name="UserName")]
public String UserName { get; set; }


[Required]

[DataType(DataType.Password)]

[Display(Name="Password")]
public string Password { get; set; }

internal bool IsValid(string p, string p_2)

{

if (p == "sahil")

return true;

else

return false;

}

}



Controller(Home)

public ActionResult Index()

{

return View();

}


[HttpPost]

[AllowAnonymous]
public ActionResult Index(Models.Login ln, string a)

{

if (a == "Submit")

{

if (ModelState.IsValid)

{

if (ln.IsValid(ln.UserName, ln.Password))

{

return RedirectToAction("Welcome", "Home");

}

else

{

ModelState.AddModelError("", "Login data is incorrect!");

}

}

return View(ln);

}

else { return RedirectToAction("Register", "Home"); }

}





View(index.cshtml)

@model MvcApplication1.Models.Login

@{ViewBag.Title = "Index";}

@using (Html.BeginForm())



{

@Html.AntiForgeryToken()

@Html.ValidationSummary(true, "Login failed. Check your login details.");

<fieldset >

<legend>Login </legend>

<div>

<div>

<div>@Html.LabelFor(m=>m.UserName)</div>

<div> @Html.TextBoxFor(m=>m.UserName)</div>

<div> @Html.ValidationMessageFor(m=>m.UserName)</div>

</div>

<div>

<div>@Html.LabelFor(m=>m.Password )</div>

<div> @Html.TextBoxFor(m => m.Password)</div>

<div> @Html.ValidationMessageFor(m => m.Password)</div>

</div>

<div>

<div><input type="submit" id="btnSubmit" name="a" value="Submit" />

<input type="submit" id="btnRegister" name="a" value="New User" /></div>

</div>

</div>










</fieldset>



}


No comments:

Post a Comment