简体   繁体   English

如何使用“编辑”表单中的敲除数据绑定将动态ASP.NET DropDownListFor()与数据库中的存储值绑定

[英]How do you bind your dynamic ASP.NET DropDownListFor() with the stored value in the database using knockout data binding in the Edit form

I have a working Form which dynamically fetches the data from the database, updates it and stores it in the database.However, I have an issue in dynamically binding a dropdown picklist to the current value stored in the database while Editing. 我有一个可以正常工作的表单,可以动态地从数据库中获取数据,更新数据并将其存储在数据库中。但是,在编辑时将下拉式选择列表动态绑定到数据库中存储的当前值存在问题。

The issue I face is with the picklist dropdown field, when it is loaded in the Edit form, it is not bound to the current value of that field in the database, as a work around to it, I create a disabled field with the current value in the database, and a second dropdown field for the picklist. 我面临的问题是选择列表下拉字段,当它以“编辑”形式加载时,它没有绑定到数据库中该字段的当前值,为此,我创建了一个禁用字段,其中包含当前数据库中的值,以及选择列表的第二个下拉字段。 The edit form on the ajax post picks up the value in the dropdown field and stores it into the database. Ajax帖子上的编辑表单将在下拉字段中获取值,并将其存储到数据库中。 Now you can guess the issue is that when the user does not even touch the dropdown field from the picklist, the default value gets posted and gets stored in the database hence editing it without the user noticing. 现在您可以猜到问题是,当用户甚至没有触摸选择列表中的下拉字段时,默认值就会被发布并存储在数据库中,从而在用户不注意的情况下对其进行编辑。

<div class="row" id="editProspectMeasuredStructurFormDiv" data-bind="with:EditModel">


@using (Html.BeginForm("Edit", "ProspectMeasuredStructure", FormMethod.Post, new { @class = "form-group", @id = "editForm" })){
      @Html.TextBoxFor(m => m.MeasuredStructureId, new { @class = "form-control", data_bind = "value: MeasuredStructureText", disabled = "disabled" })
<label>The MeasuredStructureis listed as:</label>

     @Html.DropDownListFor(m => m.MeasuredStructureId, new SelectList(Model.MeasuredStructures, "MeasuredStructureId", "Name"), new { @class = "form-control", @id = "editMeasuredStructureId", placeholder = Html.DisplayNameFor(m => m.MeasuredStructureId) })
     @Html.LabelFor(m => m.MeasuredStructureId)
     <input type="submit" class="btn btn-action" value="Submit" id="submitEditMeasuredStructure" data-bind="click: function(){$root.EditMeasuredStructure();}" />
}
</div>
 var prospectMeasuredStructureModel = function (parent, prospectMeasuredStructureId, prospectId, prospectText, measuredStructureId, measuredStructureText{
 var self = this;
        self.ProspectMeasuredStructureId = ko.observable(prospectMeasuredStructureId);
        self.ProspectId = ko.observable(prospectId);
        self.ProspectText = ko.observable(prospectText);
        self.MeasuredStructureId = ko.observable(measuredStructureId);
        self.MeasuredStructureText = ko.observable(measuredStructureText);
        self.EditModel = ko.observable();
}



var prospectMeasuredStructureViewModel = function () {
        var self = this;
        self.ProspectMeasuredStructureId = ko.observable();
        self.ProspectId = ko.observable();
        self.ProspectText = ko.observable();
        self.MeasuredStructureId = ko.observable();
        self.MeasuredStructureText = ko.observable();
        self.EditModel = ko.observable();

        self.EditMeasuredStructure = function () {
            self.ProspectMeasuredStructureId = $('#editProspectMeasuredStructureId').val();
            self.ProspectId = $('#hfProspectId').val();
            self.MeasuredStructureId = $('#editMeasuredStructureId').val();
            self.Address = $('#editAddress').val();

            $.ajax({
                url: '@Url.Action("Edit", "ProspectMeasuredStructure")',
                type: 'POST',
                data: JSON.stringify(self),
                contentType: 'application/json; charset=utf-8',
                dataType: 'JSON',
                success: function (response) {
                    if (response.success) {
                        self.GetMeasuredStructures();
                        $('#editForm').hide();
                        alert("Edit Worked");
                    } else {
                        alert("Edit did not work");
                    }
                }
            });
        }
  [HttpPost]
        public ContentResult GetMeasuredStructures([FromBody] ProspectMeasuredStructureViewModel measuredStructureVM)
        {
            //Guid ProspectId = Guid.Parse(measuredStructure.ProspectId);
            var prospectMS = repository.Get(c => !c.Delete && c.ProspectId== measuredStructureVM.ProspectId);
            foreach (var item in prospectMS)
            {
                ProspectMeasuredStructureViewModel pVM = new ProspectMeasuredStructureViewModel();
                pVM.ProspectMeasuredStructureId = item.ProspectMeasuredStructureId;
                pVM.ProspectId = item.ProspectId;
                pVM.ProspectText = listManager.GetProspect(item.ProspectId).Name;
                pVM.MeasuredStructureId = item.MeasuredStructureId;
                pVM.MeasuredStructureText = listManager.GetMeasuredStructure(item.MeasuredStructureId).Name;
                prospectMeasuredStructureVModels.Add(pVM);
            }
            return new ContentResult()
            {
                Content = JsonConvert.SerializeObject(prospectMeasuredStructureVModels),
                ContentType = "application/json"
            };
        }

I am not sure how to bind the @Html.DropDownListFor() with the saved value which was added when the user first added the measured structure. 我不确定如何将@Html.DropDownListFor()与保存的值绑定,该保存的值是在用户首次添加测量的结构时添加的。 As I said before, it shows the picklist correctly, what it does not do is "show the currently saved value as the first value". 如前所述,它正确显示了选择列表,但它没有执行的操作是“将当前保存的值显示为第一个值”。 Hence it causes the default value to be saved to the database. 因此,它将导致默认值保存到数据库。

Kindly let me know if there is more code, if that needs to be posted, as I tried to take as many snippets as I thought were sufficient to explain my issue. 请让我知道是否还有更多代码,是否需要发布,因为我尝试了尽可能多的代码片段来解释我的问题。

All I had to do was bind the dropdown list with the ID and that translated as the default value of the drop down list along with other picklist values in my view where the DropDownList was being declared. 我所要做的就是将下拉列表与ID绑定在一起,并将其转换为下拉列表的默认值以及声明了DropDownList的视图中其他选择列表的值。

  @Html.DropDownListFor(m => m.MeasuredStructureId, new SelectList(Model.MeasuredStructures, "MeasuredStructureId", "Name"), new { @class = "form-control", @id = "editMeasuredStructureId", placeholder = Html.DisplayNameFor(m => m.MeasuredStructureId),data_bind = " value: MeasuredStructureId" })
  @Html.LabelFor(m => m.MeasuredStructureId)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM