Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
36
rated 0 times [  43] [ 7]  / answers: 1 / hits: 18555  / 11 Years ago, sat, april 27, 2013, 12:00:00

I'm new to MVC(Asp.net) as well as JavaScript. sorry for this primary question.



<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.



<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?


More From » asp.net-mvc

 Answers
43
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:



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.


[#78569] Friday, April 26, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monicag

Total Points: 651
Total Questions: 106
Total Answers: 104

Location: Grenada
Member since Sun, Dec 20, 2020
4 Years ago
monicag questions
;