Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my model.
C#
public class Model1
    {
        [Required(ErrorMessage="Username must not be blank")]
        public string Username { get; set; }
        [Required(ErrorMessage="Password should match")]
        public string Password { get; set; }
    }


This is my controller.
SQL
public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ViewResult Index(Model1 model1)
        {
            if (ModelState.IsValid)
            {
                if (model1.Username == "adm" && model1.Password == "adm")
                {
                    return View("FWMenu", model1);
                }
                else
                {
                    return View();
                }
            }
            return View();
        }


This is my view.

SQL
<h1>
		Log-in</h1>
	<br />
	@Html.ValidationSummary()
	@using (@Html.BeginForm())
 {
		<p>@Html.TextBoxFor(x => x.Username)</p>
		<p>@Html.TextBoxFor(x => x.Password)</p>
		<br />
		<p>
			<input type="submit" value="Login" /></p>
		
 }
</div>


My validation is working perfectly. My problem is that when i am keeping my username and password field blank and clicking on login, the whole page is getting refreshed to show me the validation. But i do not want my full page to get refreshed. Is it possible? If yes then what is the solution?
Posted
Comments
Arkadeep De 4-Jun-15 2:53am    
why don't you use jquery post to do login...it would not let the page to be refreshed.

1. One solution is to use partial view or a panel for your login controls (username, password and their validations) and AJAX call for re-rendering only the partial view/panel used for user login controls.

2.You can find example for a similar solution in the next article:
http://www.aspdotnet-pools.com/2014/09/ajax-login-form-using-jquery-in-aspnet.html[^]
 
Share this answer
 
v2
Comments
Member 11710169 4-Jun-15 3:20am    
Its done. Thank you for the solution.
Raul Iloc 4-Jun-15 3:38am    
Welcome, I am glad that I could help you!
Member 11710169 4-Jun-15 3:42am    
Hey Raul, can you please help me in another way? Can you explain that why we use custom validation?
Raul Iloc 4-Jun-15 3:57am    
The custom validation is used when you have to validate a user input for a control, and the existing validation control cannot help you. For example in my next article http://www.codeproject.com/Articles/526827/MVC-Basic-Site-Step-Multilingual-Site-Skeleton I used it for validation email address, but there are a lot of article about this subject.
Member 11710169 4-Jun-15 7:11am    
Thank you so much Mr. Raul
you can use jquery.validate.js and jquery.validate.unobtrusive.js on your webpage to display client side error's for model validations.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900