javascript asp.net mvc 从 Html.TextBoxFor() 获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16256290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
asp.net mvc get value from Html.TextBoxFor()
提问by DevT
I'm new to MVC(Asp.net) as well as JavaScript. sorry for this primary question.
我是 MVC(Asp.net)以及 JavaScript 的新手。抱歉这个主要问题。
<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>
I need to get t.CustomerName
value using JavaScript. I tried as below and it gives me errors.
我需要t.CustomerName
使用 JavaScript获取价值。我尝试如下,它给了我错误。
<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>
i wrote my controller coding as below :
我写了我的控制器编码如下:
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);
}
error:
错误:
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.
编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。
I searched and I found similar question asp.net mvc get value from Html.textbox()here. but it doesn't discussed about how to get value from Html.TextBoxFor
so how can I do this?
我搜索并发现了类似的问题asp.net mvc get value from Html.textbox()here。但它没有讨论如何从中获取价值,Html.TextBoxFor
所以我该怎么做?
回答by Ufuk Hac?o?ullar?
var value = document.getElementById('<%=t.CustomerName%>').value;
This line is causing the error. ASP.NET MVC doesn't generate random client side ids like in Web Forms. They will be the same property name in your model. This line is what you are looking for:
此行导致错误。ASP.NET MVC 不会像在 Web 窗体中那样生成随机的客户端 ID。它们将在您的模型中具有相同的属性名称。这一行就是你要找的:
var value = document.getElementById('CustomerName').value;
Also HTML helpers like TextBoxFor()
take some delegates as a parameter. t
variable you are trying to use is meaningful in the context of a lambda expression(a short way to create delagates and expressions). If you want to reach a model property, use Model
variable.
HTML 助手也喜欢TextBoxFor()
将一些委托作为参数。t
您尝试使用的变量在 lambda 表达式的上下文中是有意义的(创建 delagates 和表达式的一种简短方法)。如果要达到模型属性,请使用Model
变量。
回答by Tim Medora
A clean way to get the ID is to use IdFor()
. This allows hierarchical IDs to be resolved and avoids hardcoding the string name for the property into your code.
获取 ID 的一种干净方法是使用IdFor()
. 这允许解析分层 ID,并避免将属性的字符串名称硬编码到您的代码中。
function CheckName() {
var value = document.getElementById('<%:Html.IdFor(t => t.CustomerName) %>').value;
alert(value);
}