我有一个输入表单,要求用户输入其用户名和密码。一旦用户输入数据,然后单击提交按钮,jquery将运行一个post请求并将信息发送到控制器/ Home / RegisterUser,但所有即将到来的
在email2.dll中发生了'System.NullReferenceException'类型的异常,但未在用户代码中处理
附加信息:未将对象引用设置为对象的实例。
由于某种原因,ajax请求无法获取值email和密码,因为它们被设置为null,即email = null。
版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.0.30319.18446
WebForm1.aspx的
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="email2.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.10.2.js" type="text/javascript" ></script>
</head>
<body>
<form id="form1" runat="server" >
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<fieldset style="width: 200px;">
<asp:Label ID="lblEmailAddress" runat="server" Text="Email Address"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</fieldset>
<div>
</div>
<asp:Button ID="btnCreateAccount" runat="server" Text="Signup" OnClientClick="Signup();return false;" />
</div>
<div id="postResult">?</div>
</form>
</body>
</html>
// use PageMethods. if want to use methods within WebForm1.apsx.cs
<%--<script type="text/javascript">
function Signup() {
var email = document.getElementById('<%=txtEmail.ClientID %>').value;
var password = document.getElementById('<%=txtPassword.ClientID %>').value;
PageMethods.RegisterUser(email, password, onSucess, onError);
function onSucess(result) {
alert(result);
}
function onError(result) {
alert('Cannot process your request at the moment, please try later.');
}
}
</script>--%>
<%--<script type="text/javascript">
var url = "/Home/RegisterUser";
var email = document.getElementById('<%=txtEmail.ClientID %>').value;
var password = document.getElementById('<%=txtPassword.ClientID %>').value;
var f = $("form1");
var url = f.attr("action");
var formData = f.serialize();
$.post(url, formData, function (data) {
$("#postResult").html(data);
});
</script>
--%>
<script type="text/javascript">
var username = $("input#txtEmail").val();
var password = $("input#txtPassword").val();
$(document).ready(function () {
$("#btnCreateAccount").click(function () {
debugger;
$.ajax({
type: 'POST',
url: 'Home/RegisterUser',
data: { username: username, password: password },
ContentType: 'application/Json',
dataType: "json",
success: function (data) {
alert("Register succesfully");
$("#postResult").html(data);
},
error: function () {
alert("Error while registration");
}
});
});
});
</script>
HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Services;
//need this to run [WebMethod]
using System.Web.Services;
namespace email2.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
[WebMethod]
[AcceptVerbs(HttpVerbs.Post)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public ActionResult RegisterUser(string email, string password)
{
string result = "Congratulations!!! your account has been created.";
if (email.Length == 0)//Zero length check
{
result = "Email Address cannot be blank";
}
else if (!email.Contains(".") || !email.Contains("@")) //some other basic checks
{
result = "Not a valid email address";
}
else if (!email.Contains(".") || !email.Contains("@")) //some other basic checks
{
result = "Not a valid email address";
}
else if (password.Length == 0)
{
result = "Password cannot be blank";
}
else if (password.Length < 5)
{
result = "Password canonot be less than 5 chars";
}
return Content(result);
}
}
}
答案 0 :(得分:0)
应该是
public ActionResult RegisterUser(string email, string password)
{
string result = "Yeah!";
//... some checks
return Content(result);
}
而不是
public static string RegisterUser(string email, string password)
{
//....
}
如果是static