我是新来的MVC(Asp.net)以及JavaScript。对于这个主要问题抱歉。asp.net mvc从Html.TextBoxFor()获取值
<body>
<div>
<%--<% Html.BeginForm(); %>--%>
<table>
<tr>
<td><%:Html.LabelFor(t=> t.CustomerID) %></td>
<td><%:Html.TextBoxFor(t => t.CustomerID, new { ID = "txtCustomerID" })%></td>
</tr>
<tr>
<td><%:Html.LabelFor(t=> t.CustomerName) %></td>
<td><%:Html.TextBoxFor(t => t.CustomerName, new { ID = "txtCustomerName" })%></td>
</tr>
<tr>
<td>
<input type="submit" value="Submit" onclick="CheckName()" />
</td>
</tr>
</table>
<%--<% Html.EndForm(); %>--%>
</div>
</body>
我需要使用JavaScript获得t.CustomerName
值。我尝试如下,它给我错误。
<head runat="server">
<script type="text/javascript">
function CheckName() {
var value = document.getElementById('<%=t.CustomerName%>').value;
alert(value);
}
function SubmitData() {
var obj = {};
obj.CustomerID = $("#txtCustomerID").val();
obj.CustomerName = $("#txtCustomerName").val();
$.ajax({
url: "CreateNewRetailCustomer?jsonObject=" + JSON.stringify(obj),
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (result) {
if (result.status == "successful") {
document.location.href = '../';
}
}
});
}
</script>
<title>Create Wholesale Customer</title>
</head>
我写我的控制器如下编码:
public ActionResult CreateRetailCustomer()
{
return View();
}
[HttpPost]
public ActionResult CreateRetailCustomer(RetailCustomer retCust)
{
new CustomerService.CustomerServiceClient().SaveRetailCustomer(retCust);
return RedirectToAction("Index");
}
[HttpPost]
public JsonResult CreateRetailCustomer(string jsonObject)
{
var retailCustomer = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Models.RetailCustomer>(jsonObject);
new CustomerService.CustomerServiceClient().SaveRetailCustomer(retailCustomer);
return Json(new { status = "successful" }, JsonRequestBehavior.AllowGet);
}
[HttpGet]
public JsonResult GetCustomerList()
{
var custlist = new CustomerService.CustomerServiceClient().GetCustomer().Select(m => new { CustomerId = m.CustomerId, CustomerName = m.CustomerName});
return Json(custlist, JsonRequestBehavior.AllowGet);
}
错误:
An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
我搜索,我发现类似的问题asp.net mvc get value from Html.textbox()这里。但它没有讨论如何从Html.TextBoxFor
获得价值,所以我该如何做到这一点?
http://tilr.blogspot.com/2013/04/read-and-write-data-in-mvc-using-n-tier.html – DevT 2013-04-28 22:10:46