【问题标题】:Edit an Model and post it to Controller编辑模型并将其发布到控制器
【发布时间】:2015-07-24 09:02:18
【问题描述】:

我有一个具有 10 个不同属性的模型,我现在想在网格中编辑数据,当我连续单击“编辑”时,它会打开一个模态编辑窗口。 但是我怎样才能将编辑后的模型传递给动作,而不仅仅是 2 个属性

public class Bestellungen
{
    public virtual int BID { get; set; }
    public virtual int UID { get; set; }
    public virtual int PID { get; set; }
    public virtual int Shop_SID { get; set; }
    public virtual string er { get; set; }
    public virtual string cker { get; set; }
    public virtual float ten { get; set; }
    public virtual float en { get; set; }
    public virtual Boolean ueck { get; set; }
    public virtual DateTime Datum { get; set; }
}

在我的部分视图表单中编辑以下属性中的 2 个:

        <form id="FormId" style="padding: 20px">
            <div class="form-group">
                <label class="sr-only" for="form-cker">cker</label>
                @Html.TextAreaFor(model => model.cker, new { @cols = "40", @rows = "3", @id = "form-cker" })
            </div>
            <div class="form-group">
                <label class="sr-only" for="form-er">er</label>
                @Html.TextAreaFor(model => model.er, new { @cols = "40", @rows = "3", @id = "er" })
            </div>
            <button type="submit" id="approve-btn" class="btn btn-danger">          save</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">      canncel</button>
        </form>
    </div>
</div>

<script>
    $(document).ready(function () {
        $("#approve-btn").on("click", function () {
            $.ajax({
                url: "/Home/edit",
                type: "POST",
                data: $('#FormId').serialize()
            })
            .done(function (partialViewResult) {
                $("#partial").html(partialViewResult);
                $('#modal-container').modal('hide');
            });
        });
    });
</script>

【问题讨论】:

    标签: asp.net view model


    【解决方案1】:

    我认为,您可以将序列化的“大”表单与data: $('#FormId').serialize() 合并。但这不是美丽的解决方案。这是一个拐杖。好主意是在控制器中创建另一个动作,它只保存这两个字段。这样做怎么样?

    【讨论】:

    • 看,如果你在“主页/编辑”操作中添加一些关于保存其他字段的新逻辑,你会在保存这两个字段时遇到问题。这是非常糟糕的情况。我真的建议你添加另一个动作。
    • 但是如果我只发送这两个字段,我怎么能保存我的其他属性,如 PID、UID?
    • 这个模型是否存储在数据库中?您只能将 ID(我猜是从大表格发送)发送到操作。但并非所有字段。
    • 是的,它存储在数据库中,我在控制器中使用 Nhibernate 对其进行更新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2012-12-18
    相关资源
    最近更新 更多