Ajax in Visual Studio 2013 undefined
Publicado por Jhon Jairo (4 intervenciones) el 23/06/2018 00:17:49
Hola, uso VS 2013, tengo una tabla que debería mostrar los datos de un formulario, pero muestra undefined
HomeController
UserModel
Index
HomeController
1
2
3
4
5
6
7
8
9
10
11
[HttpPost]
public ActionResult AddUser(string UserName, string Email, string Password)
{
var user = new UserModel();
user.UserName = UserName;
user.Email = Email;
user.Password = Password;
return Json(user, JsonRequestBehavior.AllowGet);
}
UserModel
1
2
3
4
5
6
7
8
public class UserModel
{
public int ID { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
Index
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
@{
ViewBag.Title = "Home Page";
}
@model AjaxTut.Models.UserModel
<div class="row">
<div class="col-md-8 col-lg-offset-4">
<h2>Add User</h2>
<br />
<span>User Name: </span> @Html.TextBoxFor(x => x.UserName, new { @class = "form-control" })<br />
<span>Email: </span> @Html.TextBoxFor(x => x.Email, new { @class = "form-control" })<br />
<span>Password: </span> @Html.TextBoxFor(x => x.Password, new { @class = "form-control" })<br />
<p><a class="btn btn-default" onclick='SaveUser()'>Save</a></p> <br /><br /><br />
</div>
</div>
<div class="row">
<div class="col-md-8 col-lg-offset-3">
<h2>New User List</h2>
<table id="myTable" class="table-striped">
<tr>
<th>User Name</th>
<th>Email</th>
<th>Password</th>
</tr>
</table>
</div>
</div>
<style>
#myTable tr th {
color: white;
width: 300px;
height: 40px;
text-decoration: solid;
background-color: yellowgreen;
padding: 10px;
}
</style>
<script>
function SaveUser() {
var userName = $("#UserName").val();
var email = $("#Email").val();
var password = $("#Password").val();
$.ajax({
type: "POST",
ur: "Home/AddUser",
data: JSON.stringify({ UserName: userName, Email: email, Password: password }),
contentType: "application/json",
success: function (result) {
$("#myTable").append("<tr><td>" +
result.UserName + "</td><td>" +
result.Email + "</td><td>" +
result.Password + "</td></tr>");
$("#UserName").val("");
$("#Email").val("");
$("#Password").val("");
}
})
}
</script>
Valora esta pregunta


0