rikket
rikket

Reputation: 2407

Get MVC Form Data Using Javascript

I have the following form using MVC :

@using (Html.BeginForm())
    {
        <div class="editor-label">
            @Html.LabelFor(m => m.Title)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.Title)
            @Html.ValidationMessageFor(m => m.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(m => m.Description)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(m => m.Description)
            @Html.ValidationMessageFor(m => m.Description)
        </div>

         <div class="editor-label">
            @Html.LabelFor(m => m.Icon)
        </div>
        <div class="editor-field">
            @Html.RadioButtonFor(m => m.Icon, "http://maps.google.com/mapfiles/ms/icons/red-pushpin.png", new { @checked = "checked" }) <img src="http://maps.google.com/mapfiles/ms/icons/red-pushpin.png" alt="red pusphin" />
            @Html.RadioButtonFor(model => model.Icon, "http://maps.google.com/mapfiles/ms/icons/red-pushpin.png") <img src="http://maps.google.com/mapfiles/ms/icons/blue-pushpin.png" alt="blue pushpin" />
        </div>

    }

How can I retrieve the data using javascript?

I tried the following but it does not seem to be working:

document.getElementsByName('Title').value;

Upvotes: 1

Views: 2313

Answers (3)

super
super

Reputation: 2328

If you need the values from the model to javascript you can simply do this.

function showpopup()
{
   var model = @Html.Raw(Json.Encode(Model));
   alert(model.Title);
}

Upvotes: 0

Nilesh Gajare
Nilesh Gajare

Reputation: 6398

try this

document.getElementById('Title').value;

Check this fiddle

http://dotnetfiddle.net/t67Q3G

Upvotes: 2

user3497034
user3497034

Reputation:

Define ID for all controls as:

@Html.LabelFor(m => m.Title,new{@ID="YOUR ID"})

Get value by id as:

$("#YOUR ID").val();

Upvotes: 0

Related Questions