Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My application contains two text boxes and one action link. On click of action link, values gets populated in text boxes. Values are showing in UI. But, those are not bind with model and showing validation error messages, though the text boxes have values. Please let me know how to avoid those error messages

Note: As there are several fields in model and view, I listed few here.

View:

JavaScript
 <div id="divNames">
@Html.TextBoxFor(model => model.Name, new { id = "txtFirstName", maxlength = "30" })
  @Html.TextBoxFor(model => model.LastName, new { id = "txtLastName", maxlength = "30" })</div>
  @Html.ActionLink("Search", "", "", null, new { id = "btnSearch")

  $("#btnSearch").click(function (e) {
   var url = '@Url.Content("~/")' + "signature/GetDetails";
           var idnumber = $("#txtDocumentNo").val();
           $.ajax({
               url: url,
               data: $('#frm1').serialize(),
               type: 'GET',
               success: function (data) {
                     var url = '@Url.Content("~/")' + "signature/GetDetails";
                     $('#divNames').load(url2 + ' #divNames')
                   }
               });
               return false;
       });

Controller:
C#
public ActionResult GetDetails(User user)
{
       user.Name = "first name";
       user.LastName = "last name";

       return View("Registration", user);
}

Model:
C#
public class BRUser
{
    public string Name { get; set; }
    public string LastName { get; set; }
}
Posted

Your AJAX call code is causing issue as it is not appropriate.
In Section "Uploading Image by Browsing File" below webpage a Model (with two string properties) is being passed to controller successfully by using AJAX call.
Uploading Image by File Browsing, Dragging & Dropping and Using Webcam in ASP.NET MVC[^]

You need to modify code accordingly. Thanks.
 
Share this answer
 
Hi Narvish,
First thing I would like to bring here is you are posting the data so the ajax type should be "POST" with a [HttpPost] attribute on the ActionMethod and also check if the Form identifier is frm1.

Your ajax should look like:-
JavaScript
$.ajax({
url:'THE URL',
type:"POST",
data:$("#frm1").serialize(),
success:function(data){

    }
});

Check for the form also
C#
<form id="formPost"> //Please change the identifier name using numerics should be avoided
....
....
</form>

I hope you get me here.
Please post back your queries if any.
Thanks.
 
Share this answer
 
Comments
NarVish 2-Jan-15 0:23am    
I tried with POST in ajax and [HTTPPost] attribute on the action method. This time I'm not getting values at all. My form is like this below.
@using (Html.BeginForm("Registration", "signature", FormMethod.Post, new { id = "frmTest" }))
{
div id="divNames">
@Html.TextBoxFor(model => model.Name, new { id = "txtFirstName", maxlength = "30" })
@Html.TextBoxFor(model => model.LastName, new { id = "txtLastName", maxlength = "30" })</div>
@Html.ActionLink("Search", "", "", null, new { id = "btnSearch")
}
[no name] 2-Jan-15 0:37am    
If you are using HTML.beginform then no need of ajax call this would search for post action method called Registration with controller Signature on click of submit
NarVish 2-Jan-15 3:05am    
Because of network issues, I couldn't able to post this comment immediately. I removed ajax call and my action link is @Html.ActionLink("Search", "GetDetails", "signature", null, new { id = "btnSearch"}). HTTPPost method didn't get invoked in controller. For testing purpose, I created HTTPGet method with the same name, it got invoked
[no name] 2-Jan-15 3:09am    
what is that signature there?? Is that an area??
NarVish 2-Jan-15 3:11am    
signature is controller name

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900