帖子ID在表格中不起作用

时间:2013-04-05 07:21:29

标签: asp.net-mvc-3

我有一个帖子,我定义了一个新的数据库实体。用户输入所需的值并提交表单后,我插入实体并将其发回到同一页面并显示其ID。当我按如下所示输入ID区disabledreadonly时,它的效果非常好。

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>

    ID:
    @Html.TextBoxFor(model => model.ID, new { disabled = "disabled", @readonly = "readonly" })

    <p><input type="submit" name="submit" value="Create" /></p>
    </fieldset>
}

但是,当我只输入readonly时,如下所示,它会插入实体,但不会查看ID。

@Html.TextBoxFor(model => model.ID, new { @readonly = "readonly" })

我在表单中有另一个提交,它使用实体ID。但是,我不能在这里使用disabled ID区域,因为它不会发布ID值。我该怎么做才能解决这个问题?

2 个答案:

答案 0 :(得分:1)

您可以在提交之前启用disabled字段。因此,请禁用ID字段。现在,在提交时启用它。即;如果您在“myButton”上提交表单,请在document.ready()

中添加
$('#myButton').click(function(e)
{
    e.preventDefault();
    $("#ID").removeAttr('disabled');
    $('form').submit();
});

答案 1 :(得分:0)

我没有使用html扩展,而是直接写道:

<input type="text" name="ID" value="@Model.ID" readonly />

它就像一个魅力。

相关问题