简体   繁体   English

将复杂类型传递给Bootstrap模式

[英]Pass complex type to Bootstrap modal

The project is an ASP MVC 6 application. 该项目是ASP MVC 6应用程序。 I'm trying to pass a class object to Bootstrap modal dialog. 我正在尝试将类对象传递给Bootstrap模式对话框。 I went through some articles describing how data can be passed to the modal dialog using JQuery script. 我阅读了一些文章,描述了如何使用JQuery脚本将数据传递给模式对话框。 The examples cover passing simple data types like an int or string value but my luck could not get one with complex types. 这些例子涵盖了传递简单数据类型,如int或string值,但我的运气无法获得具有复杂类型的数据类型。

As a workaround I tried serializing the class object to json string and could successfully pass it to the modal dialog (code snippet provided). 作为一种解决方法,我尝试将类对象序列化为json字符串,并可以成功将其传递给模式对话框(提供的代码片段)。 But, I'm stuck up at deserializing the json to ViewModel object and further updating the controls with the object values in the modal dialog. 但是,我坚持将json反序列化为ViewModel对象,并使用模态对话框中的对象值进一步更新控件。 could someone provide some directions in solving the issue? 有人可以提供解决问题的方向吗?

HTML (ASP Razor view) HTML(ASP Razor视图)

<button role="button" data-toggle="modal" data-target="#updateModal" data-dataid=@JsonHelper.SerializeObject(@item) />

JQuery script JQuery脚本

$("#updateModal").on('show.bs.modal', function (e) {
    var dataid = e.relatedTarget.dataset.dataid;

    //populate the textbox - to test data is passed properly
    $(e.currentTarget).find('textarea[name="dataidval"]').val(dataid);
 });

Am I on the right path in achieving what I wanted? 我是在正确的道路上实现我想要的吗? Any better/alternative solutions? 任何更好/替代解决方案?

I gave a try and could achieve what I wanted. 我试了一下,可以实现我想要的。 I'm still eager to find out a better solution though. 我仍然渴望找到更好的解决方案。 I've tried to explain what worked for me. 我试图解释一下对我有用的东西。

The solution that worked for me is, deserialize the json object (complex type) and assign the values to the controls individually in the modal show callback event. 对我有用的解决方案是,对json对象(复杂类型)进行反序列化,并在模态show callback事件中单独将值分配给控件。 The name attribute of the controls should match the ViewModel type to get the values in form submission. 控件的name属性应与ViewModel类型匹配,以获取表单提交中的值。 The values can still be submitted separately as name-value pair of the controls if you do not wish to pass complex type to Action method. 如果您不希望将复杂类型传递给Action方法,则仍可以将这些值作为控件的名称 - 值对单独提交。

JQuery script JQuery脚本

$("#updateModal").on('show.bs.modal', function (e) {

    var dataid = e.relatedTarget.dataset.dataid;

    //deserialize json
    var dataObject = JSON.parse(dataid);

    //populate the controls with values        
    $(e.currentTarget).find('input[name="ViewModel.ID"]').val(dataObject.ID);
    $(e.currentTarget).find('input[name="ViewModel.Name"]').val(dataObject.Name);
    $(e.currentTarget).find('input[name="ViewModel.PropertyA"]').val(dataObject.PropertyA);
    $(e.currentTarget).find('input[name="ViewModel.PropertyB"]').val(dataObject.PropertyB);
 });

HTML (bootstrap modal) HTML(引导模式)

<!-- minimal code for the sake of clarity -->
@model ViewModel

<form asp-controller="XYZController" asp-action="@nameof(XYZController.Update)" asp-route-returnurl="@ViewData["ReturnUrl"]"
  method="post" class="form-horizontal" role="form">

  <div class="form-group">

        <input asp-for="@Model.ID" name="ViewModel.ID" class="hidden" />
        <!-- asp-for attributes may not have any effect in this case-->
        <input class="form-control" asp-for="@Model.Name" name="ViewModel.Name" />
        <input class="form-control" asp-for="@Model.PropertyA" name="ViewModel.PropertyA" />
        <input class="form-control" asp-for="@Model.PropertyB" name="ViewModel.PropertyB" />

  </div>
</form>

ASP MVC Controller ASP MVC控制器

    [HttpPost]
    public IActionResult Update(ViewModel viewModel)
    {
        return View();
    }

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

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