Anup
Anup

Reputation: 9738

MVC - Getting Textbox value which is set by jQuery script

I am setting value for the Textbox with jQuery. I have a Textbox as follows :-

@Html.TextBoxFor(model => model.Location, new { @disabled = "disabled" })

In jQuery, I set value for this Textbox :-

$('#Location').val('xyz');

On HTTPPOST, I have following Code :-

[HttpPost]
public ActionResult Location(FormCollection form, Location mod)
{
    string getLocation = mod.Location;
    string getLocation1 = form["FromEsiLocation"];
    // Both these methods are unable to retrieve the value which is set by jQuery
}

How to get this Value?

Upvotes: 1

Views: 615

Answers (1)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18873

The disabled textboxes or any other field values can never be posted but you can use a workaround for this just take hidden field for Location and set its value and get the hidden field #Location value on post controller action .

@Html.TextBoxFor(model => model.Location, new { @disabled = "disabled", @id="MyLocation" })

@Html.HiddenFor(model => model.Location)  //take one more hidden field.

Upvotes: 1

Related Questions