Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript获取MVC表单数据_Javascript_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

使用Javascript获取MVC表单数据

使用Javascript获取MVC表单数据,javascript,asp.net-mvc,asp.net-mvc-3,Javascript,Asp.net Mvc,Asp.net Mvc 3,我使用MVC有以下表单: @using (Html.BeginForm()) { <div class="editor-label"> @Html.LabelFor(m => m.Title) </div> <div class="editor-field"> @Html.TextBoxFor(m => m.Title)

我使用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>

    }
试试这个

document.getElementById('Title').value;
检查这把小提琴


将所有控件的ID定义为:

@Html.LabelFor(m => m.Title,new{@ID="YOUR ID"})
按id获取值为:

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

如果您需要从模型到javascript的值,您可以简单地这样做

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

它是getElementById而不是Elements,我已经尝试过了,传递的值是“我丢失的小提琴”,而不是模型绑定控件上的“”,浏览器将基于模型创建id,定义您自己的id,并使用该id访问值。您的意思是在表单提交或页面加载时获取数据
function showpopup()
{
   var model = @Html.Raw(Json.Encode(Model));
   alert(model.Title);
}