1

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;
2
  • on binding control with model, browser will create id based on model, define your own id and access value using that id. Commented Apr 18, 2014 at 10:10
  • You mean get the data on form submit or on page load Commented Apr 18, 2014 at 10:17

3 Answers 3

2

try this

document.getElementById('Title').value;

Check this fiddle

http://dotnetfiddle.net/t67Q3G

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

2 Comments

it is getElementById not Elements and I have tried already, the value being passed is ""
the fiddle worked as I was missing " " instead of ' '
0

Define ID for all controls as:

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

Get value by id as:

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

Comments

0

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);
}

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.