This is a migrated thread and some comments may be shown as answers.

Posting data to .Net MVC controller

2 Answers 447 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jan
Top achievements
Rank 1
Jan asked on 18 Jun 2012, 03:25 PM
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 Answers, 1 is accepted

Sort by
0
Vesselin Obreshkov
Top achievements
Rank 2
answered on 24 Jun 2012, 01:11 AM
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>



0
Jan
Top achievements
Rank 1
answered on 28 Jun 2012, 07:28 AM
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?
	{

	}
Tags
General Discussions
Asked by
Jan
Top achievements
Rank 1
Answers by
Vesselin Obreshkov
Top achievements
Rank 2
Jan
Top achievements
Rank 1
Share this question
or