1

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?

1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

here no errors. but doesn't popup/ display alert. why is that?
4

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.

function CheckName() {
    var value = document.getElementById('<%:Html.IdFor(t => t.CustomerName) %>').value;
    alert(value);
}

3 Comments

here no errors. but doesn't popup/ display alert. why is that?
Is the function being called correctly? Try just alerting a static string alert("test") and see if that gets called.
if i put alert("test") it works properly. but not work as u mentioned

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.