Posting data to .Net MVC controller

3 posts, 0 answers
  1. Jan
    Jan avatar
    58 posts
    Member since:
    May 2010

    Posted 18 Jun 2012 Link to this post

    Hi, 
    I have a question regarding posting data to MVC controller (action).
    If we use Razor, what would be the best practice?
    Could you give me some example for let's say login page? Something like this?

    html: what is the best practice using Kendo UI, HTML5, MVC?

    <div id="view">
    
     <input type="text" data-bind="value: UserName" required  />
    
     <input type="text" data-bind="value: Password" required  />
    
      <button data-bind="click: LogOn">Log On</button>
    
     </div>
    
     <script>
    
    	$(document).ready(function () {
     		var validatable = $("#view").kendoValidator().data("kendoValidator");
    		var viewModel = {
     			LogOn: function () {
    			var userName = this.get("UserName");
     			var password = this.get("Password");
     				if (validatable.validate()) {
    				//What should be done here in order to send username and password?
    				}
    
    			}
    
    		};
    
    		kendo.bind($("#view"), viewModel);
    
    	});
     </script>

    C# code: how this part should look like?

    public class AccountController : Controller
    {
    	[HttpPost]
    	public ActionResult LogOn(LogOnModel modelstring returnUrl)
    	{
    
    		if (ModelState.IsValid)
    		{
    			if (Membership.ValidateUser(model.UserNamemodel.Password))
    			{
    				FormsAuthentication.SetAuthCookie(model.UserNamemodel.RememberMe);
    				if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
    					&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
    				{
    					return Redirect(returnUrl);
    				}
    				else
    				{
    					return RedirectToAction("Index""Home");
    				}
    			}
    			else
    			{
    				ModelState.AddModelError("""The user name or password provided is incorrect.");
    			}
    		}
    		return View(model);
    	}
    }
    
    
    




  2. Vesselin Obreshkov
    Vesselin Obreshkov avatar
    81 posts
    Member since:
    Jan 2010

    Posted 23 Jun 2012 Link to this post

    This should work:
    
    <form id="logOnForm" method="post" action="@Url.Action("LogOn", "Account")">
    <div id="view">
    
     <input type="text" data-bind="value: UserName" required  />
    
     <input type="text" data-bind="value: Password" required  />
    
      <button data-bind="click: LogOn">Log On</button>
    
     </div>
    </form>
    
    
     <script>
    
    	$(document).ready(function () {
     		var validatable = $("#view").kendoValidator().data("kendoValidator");
    		var viewModel = {
     			LogOn: function () {
    			var userName = this.get("UserName");
     			var password = this.get("Password");
     				if (validatable.validate()) {
    					$("#logOnForm").submit();
    				}
    
    			}
    
    		};
    
    		kendo.bind($("#view"), viewModel);
    
    	});
     </script>



  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Jan
    Jan avatar
    58 posts
    Member since:
    May 2010

    Posted 28 Jun 2012 Link to this post

    Well, I did what you suggested and thanks for that, now the thing is that in controller the method itself should look like what?
    Attributes which are in LogOn action are:
    LogOnModel modelstring returnUrl
    Should they me something else? Because this doesn't work still.
    [HttpPost]
    	public ActionResult LogOn(LogOnModel modelstring returnUrl) - what should be here?
    	{
    
    	}
Back to Top