0

I am using asp.net mvc application. In my view page i have textbox like below code:

@Html.TextBoxFor(m => m.Id, new { @class = "form-control", @readonly = "readonly" })

Also, in form submit ajax post,

$('#btnSubmit').click(function(e)
    {
        $.ajax({
            url: '/button/Button',
            type: 'POST',
            data: {Id:1},
           });
    });

My controller:

 MasterM empcode = new MasterM();

        [HttpGet]
        public ActionResult Button()
        {

            var empcode = new MasterM();

            return View(empcode);
         }

        [HttpPost]
        public  ActionResult Button(MasterM model)
        {
            ModelState.Clear();
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            return View(model);
        }

after this form submit the value of the text box should change to new value (1). How can i achieve this in controller section itself. I know in ajax success we can manually update the element value but i want to achieve this in controller section and update model with new values.

8
  • why are sending ajax call, just do normal form post Commented Mar 14, 2017 at 8:56
  • Your model is being updated in the controller (the value of Id is 1 is the POST method). If you want to update the textbox, then use javascript, or update the DOM with the view you have returned in the success callback Commented Mar 14, 2017 at 8:56
  • No i need ajax call for some other purpose Commented Mar 14, 2017 at 8:57
  • @StephenMuecke, here for sample i have given 1 directly but in real case it will change by some other actions Commented Mar 14, 2017 at 8:58
  • There is no point using ajax if you going to return the whole view! Commented Mar 14, 2017 at 8:58

1 Answer 1

1

When you are Posting your are sending only a Single Id and not the Entire Model , So basically this

public  ActionResult Button(MasterM model) will be empty 

and since you are returning back the empty set the text box will not be updated.

There are 2 things you can do here : 1) Modify the Receiving type to

 Public  ActionResult Button(int Id)
    {
    MasterM model = new MasterM();
    model.Id = Id;
     if (!ModelState.IsValid)
                {
                    return View(model);
                }
                return View(model);
    }

2) If you want to retain Model as your Param in Controller you need to serialize the Data in Jquery and post it .

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

Comments

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.