Textbox value is not getting updated in ASP.NET MVC

I am trying to update the value of the text box after the ajax call is completed.

Here is the action controller method that is being called as a result of my ajax call:

[HttpPost]
public ActionResult Search(string productId)
{
    ReferencePeriodsModel model = new ReferencePeriodsModel()
    {
          From = "22222"
    };
    return View("Index", model);
}

@model ReferencePeriodsModel
@using (Html.BeginForm())
{
   @Html.TextBoxFor(model=>model.From, new { id ="fromValue", Value = Model.From})
}

Here is the ajax call:

$.ajax({
        type: "POST",
        url: "@Url.Action("method", "cont")",
        //dataType: "json",
        contentType: "application/json; charset=utf-8",
        
        //cache: false,
        async: true,
        success: function (data) {
           
        },
    });

My problem is that the value of the text box is not getting updated to 22222.
I tried removing Value field with no luck .

I inspected the value of model.From and it is set to 22222

What am I doing wrong?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Seems like you wanna use @Html.BeginForm(), there’s no need in that case to use an ajax call. Actually that’s the main problem, you can’t return a view using ajax calls.
You’ll find a similar example to yours here: How does @Html.BeginForm() work? and search result in Microsoft ASP.Net MVC 5 tutorial?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x