Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
Jquery Ajax调用未绑定所有模型属性 当我在下拉列表中选择任何选项时,空白表将在屏幕上生成。但是当我在空白表中插入值并按下提交时,在动作中,我得到了模型,但该模型只包含了我从Ajax中添加的表中的属性。忽略我首先选择的dropdownlist的值。若我删除那个ajax成功代码,那个么它将为模型提供正确的值,但我不会在ajax成功创建的表中获得值_Jquery_Ajax_Asp.net Mvc 4_Razor_Model Binding - Fatal编程技术网

Jquery Ajax调用未绑定所有模型属性 当我在下拉列表中选择任何选项时,空白表将在屏幕上生成。但是当我在空白表中插入值并按下提交时,在动作中,我得到了模型,但该模型只包含了我从Ajax中添加的表中的属性。忽略我首先选择的dropdownlist的值。若我删除那个ajax成功代码,那个么它将为模型提供正确的值,但我不会在ajax成功创建的表中获得值

Jquery Ajax调用未绑定所有模型属性 当我在下拉列表中选择任何选项时,空白表将在屏幕上生成。但是当我在空白表中插入值并按下提交时,在动作中,我得到了模型,但该模型只包含了我从Ajax中添加的表中的属性。忽略我首先选择的dropdownlist的值。若我删除那个ajax成功代码,那个么它将为模型提供正确的值,但我不会在ajax成功创建的表中获得值,jquery,ajax,asp.net-mvc-4,razor,model-binding,Jquery,Ajax,Asp.net Mvc 4,Razor,Model Binding,以下是我的ajax代码: $("#Award").change(function () { var selectdAward = $("#Award").val(); $('#criteriaTable').empty(); var ServiceUrl = "/Nomination/CriteriasForAward?awardId=" + selectdAward; $.ajax({ type: 'p

以下是我的ajax代码:

 $("#Award").change(function () {
        var selectdAward = $("#Award").val();
        $('#criteriaTable').empty();
        var ServiceUrl = "/Nomination/CriteriasForAward?awardId=" + selectdAward;

        $.ajax({
            type: 'post',
            url: ServiceUrl,
            contentType: "application/json; charset=utf-8",
            error: function (xhr, err) {
                alert(xhr.responseText)
            },
            success: function (data) {
                $('#criteriaTable').append('<tr><th>Id</th><th>Criteria-Description</th><th>Comment</th></tr>');

                for (var key in data) {
                    $('#criteriaTable tr:last')
                        .after('<tr><td><label>' + data[key].Id + '</label></td>' 
                        + '<td><label>' + data[key].Title + '</label></td>'
                        + '<td><input type="text" name=Model.Comments[' + key + '].Comment></td>'
                        + '<td><input type="hidden" name=Model.Comments[' + key + '].Id value=' + data[key].Id + '></td></tr>');
                }
            }
        });
    });
这是一种观点:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.Label("Nomination Category", htmlAttributes: new { @class = "control-label col-md-4" })

            <div class="col-md-8">
                @Html.DropDownListFor(model => model.AwardId, @ViewBag.Awards as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "Award" })
                @Html.ValidationMessageFor(model => model.AwardId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Resource From", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                <label> @Html.RadioButtonFor(model => model.SelectResourcesBy, "Project", new { htmlAttributes = new { @class = "form-control" } })Project</label>
                <label> @Html.RadioButtonFor(model => model.SelectResourcesBy, "Department", new { htmlAttributes = new { @class = "form-control" } })Department</label>
                @Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Project", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.DropDownListFor(model => model.ProjectID, @ViewBag.ProjectsUnderCurrentUser as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "SelectedProject" })
                @Html.ValidationMessageFor(model => model.ManagerId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Resource Name", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.DropDownListFor(model => model.UserId, @ViewBag.Resources as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "Resources" })
                @Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
            </div>
        </div>
        <br />
        <br />
        <hr />

        <div class="form-group">
            @Html.Label("Criteria", htmlAttributes: new { @class = "control-label col-md-2" })
            <br />
            <div class="form-group">
                <div id="Criterias">
                    <table border="1" id="criteriaTable"></table>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="button" value="Save Draft" class="btn btn-default" />
                <input type="submit" value="Submit" class="btn btn-default" />
                <input type="button" value="Cancel" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

请建议解决此问题的方法。请提前感谢

您的视图模型不包含名为model的属性,因此您需要修改生成html表的代码

for (var key in data) {
    $('#criteriaTable tr:last')
        .after('<tr><td><label>' + data[key].Id + '</label></td>' 
        + '<td><label>' + data[key].Title + '</label></td>'
        + '<td><input type="text" name=Comments[' + key + '].Comment></td>'
        + '<td><input type="hidden" name=Comments[' + key + '].Id value=' + data[key].Id + '></td></tr>');
}

i、 e.移除模型。输入名称属性的前缀。

不绑定所有模型属性是什么意思?什么属性?您甚至还没有显示您的模型或发布到的方法。在“提交”按钮上,“发布操作”方法将获取我们在视图中呈现的ViewModel对象。该ViewModel应包含在视图中输入的所有值。但在我的情况下,它只包含我们在ajax成功创建的文本框中输入的值ViewModel您需要的值显示相关代码!您还需要显示视图。并显示发帖返回的方法的签名。它需要是name=Comments['+key+'].Comment>etc而不是name=Model.Comments['+key+'].Comment>-您的模型不包含名为Model的属性。请注意:您可以删除新的{id=Award},只需使用$AwardId.changefunction即可{只需使用var-selectdAward=$this.val;您应该使用var-ServiceUrl='@Url.ActionCriteriasForAward,namignment';在ajax-data中:{awardId:selectdAward},
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.Label("Nomination Category", htmlAttributes: new { @class = "control-label col-md-4" })

            <div class="col-md-8">
                @Html.DropDownListFor(model => model.AwardId, @ViewBag.Awards as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "Award" })
                @Html.ValidationMessageFor(model => model.AwardId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Resource From", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                <label> @Html.RadioButtonFor(model => model.SelectResourcesBy, "Project", new { htmlAttributes = new { @class = "form-control" } })Project</label>
                <label> @Html.RadioButtonFor(model => model.SelectResourcesBy, "Department", new { htmlAttributes = new { @class = "form-control" } })Department</label>
                @Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Project", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.DropDownListFor(model => model.ProjectID, @ViewBag.ProjectsUnderCurrentUser as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "SelectedProject" })
                @Html.ValidationMessageFor(model => model.ManagerId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Resource Name", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.DropDownListFor(model => model.UserId, @ViewBag.Resources as SelectList, "Select", new { htmlAttributes = new { @class = "form-control" }, id = "Resources" })
                @Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
            </div>
        </div>
        <br />
        <br />
        <hr />

        <div class="form-group">
            @Html.Label("Criteria", htmlAttributes: new { @class = "control-label col-md-2" })
            <br />
            <div class="form-group">
                <div id="Criterias">
                    <table border="1" id="criteriaTable"></table>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="button" value="Save Draft" class="btn btn-default" />
                <input type="submit" value="Submit" class="btn btn-default" />
                <input type="button" value="Cancel" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
  [HttpPost]
        public ActionResult AddNomination(NominationViewModel model)
        {
            return RedirectToAction("Dashboard", "Dashboard");
        }
for (var key in data) {
    $('#criteriaTable tr:last')
        .after('<tr><td><label>' + data[key].Id + '</label></td>' 
        + '<td><label>' + data[key].Title + '</label></td>'
        + '<td><input type="text" name=Comments[' + key + '].Comment></td>'
        + '<td><input type="hidden" name=Comments[' + key + '].Id value=' + data[key].Id + '></td></tr>');
}