我正在使用Visual Studio 2010更新到最新版本,並將MVC3框架更新爲最新版本。我也使用jquery 1.5.1,但這個問題似乎並沒有jQuery相關。Web:文本框的值屬性與文本框中的值不一樣
我主要有如下頁面:
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css" type="text/css" />
<link href="@Url.Content("~/Content/UserMaintainance.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/UserMaintainance.js")" type="text/javascript"></script>
@model TourSystem2.Models.UserMaintainanceViewModel
@{
ViewBag.Title = "UserMaintainance";
}
@Model.Debug
<h2>User Maintainance</h2>
<div id = "EditUser"></div>
<table>
<tr id="1">
<td><button type="button" class = "deleteButton"></button></td>
<td><button type="button" class = "editButton"></button></td>
<td>John </td>
<td>john </td>
<td>A</td>
<td>1234 </td>
<td></td>
//snipped some unrelated form generation of user information showing
<tr id = "0">
<td colspan = "7"><button type = "button" id = "addButton">Add New User</button></td>
</tr>
</table>
在這種形式的Java腳本,我做的對Add按鈕如下:
$(function() {
$(':button.editButton, :button#addButton').click(function() {
var myData = $(this).parent().parent().attr("id");
$.post("/Admin/EditUser", { UserID: myData },
function (data) {
$('#EditUser').html(data);
$('#EditUser').dialog({
modal: true,
width: 400,
resizable: false,
buttons: {
"Save": function() {
$(this).dialog("close");
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
}); //end post
});
部分頁/管理/ EditUser樣子這個:
@model TourSystem2.Models.UserEditViewModel
<link href="/Content/UserEdit.css" rel="stylesheet" type="text/css" />
@Html.HiddenFor(Model => Model.UserInfo.iUserID)
<table id="useredit">
<tr>
<td>@Html.LabelFor(Model => Model.UserInfo.cLoginName, "Login Name")</td>
<td>@Html.TextBoxFor(Model => Model.UserInfo.cLoginName)</td>
</tr>
//Snip more fields
</table>
我EditUser的控制器代碼是這樣的:
public ActionResult EditUser(int UserID)
{
UserEditViewModel myUser = new UserEditViewModel();
if (UserID == 0)
{
myUser.UserInfo.iUserID = 0;
return PartialView("UserEdit", myUser);
}
else if (myUser.Load(UserID))
{
return PartialView("UserEdit", myUser);
}
else
{
return Json("Broken: " + myUser.Debug);
}
}
我的意圖是,點擊保存後,我會做另一個AJAX帖子將信息傳遞迴服務器。
到目前爲止,除了文本框的值屬性不會更改,即使框中的文本已更改,所有內容都可以正常工作。如果點擊編輯,數據將在editUser局部頁面上正確輸出......但是任何更改都不會反映在值中。
使用螢火蟲,我可以直觀地看到,對於我在屏幕上的任何文本框,該值不會改變。在「保存用戶」文章中,只返回原始值,而不是修改後的值。
我錯過了一些非常基本的東西嗎?
*編輯1 *** 我試圖做我的SaveButton事件如下:
"Save": function() {
var myUser =
{
iUserID: ('#UserInfo_iUserID').val,
cLoginName: ('#UserInfo_cLoginName').val,
};
$.ajax({
type: "POST",
url: "/admin/SaveUser",
data: myUser,
success: function (bNum) {
alert("returned" + bNum);
}
});
我收到了cLoginName舊的結果:( '#UserInfo_cLoginName')。 val
這發生在第二次POST之前。
添加編輯1 ...仍然學習系統。 – 2011-05-14 16:58:52