简体   繁体   English

试图从Razor将变量传递给Jquery

[英]Trying to pass a variable to Jquery from Razor

So I'm working on a project in ASP.net and have a situation where I've created a form that provides users a drop down menu to select from. 因此,我正在ASP.net中的一个项目上工作,并且遇到一种情况,即我已经创建了一个表单,该表单向用户提供了一个下拉菜单供您选择。 On top of this the Jquery below allows the user to add additional drop down fields. 最重要的是,下面的Jquery允许用户添加其他下拉字段。 This works. 这可行。 Now my problem stems from the first drop down has a list of institutes which works fine as its being populated from the C# in the html form. 现在我的问题来自于第一个下拉列表,其中列出了一些机构,这些机构可以很好地工作,因为它是从html形式的C#中填充的。

When the user selects to add another drop down that box is just empty. 当用户选择添加另一个下拉菜单时,该框为空。 I've tried adding the C# directly to the Jquery but this doesn't work. 我尝试将C#直接添加到Jquery中,但这不起作用。

I'm not overly experienced with ASP.net or MVC both of which I'm using for the project, what is the best way to go around passing the values into Jquery so I can add list to the drop down? 我对项目都使用ASP.net或MVC并没有过多的经验,将值传递到Jquery中以便将列表添加到下拉列表的最佳方法是什么?

Here is the code below 这是下面的代码

<script>
    $(document).ready(function () {
        var max_fields = 10; //maximum input boxes allowed
        var wrapper = $(".input_fields_wrap"); //Fields wrapper
        var add_button = $(".add_field_button"); //Add button ID

        var x = 1; //initlal text box count
        $(add_button).click(function (e) { //on add input button click
            e.preventDefault();
            if (x < max_fields) { //max input box allowed
                x++; //text box increment
                $(wrapper).append('<div><select style="padding-left: 5px; width: 100%;" class="BasicInfoFormControl" onblur="" name="restrictedInstitute[]" /></select><a href="#" class="remove_field">Remove</a></div>'); //add input box
            }
        });

        $(wrapper).on("click", ".remove_field", function (e) { //user click on remove text
            e.preventDefault(); $(this).parent('div').remove(); x--;
        })
    });
</script>

Here is the HTML 这是HTML

   <div class="col-md-3 BasicInfoFormLabelColumn">
        <label for="restrictedInstitute" class="formLabel">Restricted Institutes: (Can't Apply)</label>
   </div>
   <div class="col-md-3 input_fields_wrap">
        <select  style="padding-left: 5px; width: 100%;" class="BasicInfoFormControl" onblur="" name="restrictedInstitute[]" id="selectRestricted" />
        <option value="0">Please Select</option>
        @foreach (var item in Model.institute)
        {

            if (@item.TradingName != null && @item.TradingName != " " && @item.TradingName != "")
            {
               <option value="@item.TradingName">@item.TradingName</option>
            }
        }
        </select>
        <div class="row">
            <div class="col-md-12  BasicInfoFormRow ">
               <button class="add_field_button addMore">+Add More institutes</button>

            </div>                       
        </div>
    </div>

I've added a couple images to help clarify what I'm trying to do. 我添加了一些图像,以帮助弄清我要做什么。

Its happening because U only append select without its option inside you Jquery. 之所以会这样,是因为U只在您的Jquery内部附加select而没有其option I recommend using AJAX and partial view to achieve ur goal. 我建议使用AJAX和局部视图来实现您的目标。 First, separate the rendering of dropdownlist into another cshtml, for example the name is Institute.cshtml 首先,将dropdownlist的呈现方式分成另一个cshtml,例如,名称为Institute.cshtml

Institute.cshtml Institute.cshtml

@model xxxx.Model.Institute //model would be your institute
<select  style="padding-left: 5px; width: 100%;" class="BasicInfoFormControl" onblur="" name="restrictedInstitute[]" id="selectRestricted" />
    <option value="0">Please Select</option>
    @foreach (var item in Model)
    {
        if (@item.TradingName != null && @item.TradingName != " " && @item.TradingName != "")
        {
           <option value="@item.TradingName">@item.TradingName</option>
        }
    }
</select>

Then you call it as partialview in your HTML 然后在HTML中将其称为“部分视图”

<div class="col-md-3 input_fields_wrap">
        @Html.Partial("Institute", Model.Institute)
        <div class="row">
            <div class="col-md-12  BasicInfoFormRow ">
               <button class="add_field_button addMore">+Add More institutes</button>
            </div>                       
        </div>
    </div>

And add the partialview in your Controller 并在你的控制器中添加partialview

xxController xx控制器

PartialViewResult Institute()
{
     return PartialView();
}

This is the first step to separate your dropdownlist into another partialView The second step is calling an ajax in your add click button, and fetch the result using this partialView 这是将您的下拉列表分成另一个partialView的第一步。第二步是在您的“添加单击”按钮中调用一个ajax ,然后使用此partialView来获取结果

javascript javascript

$(document).ready(function () {
    var max_fields = 10; //maximum input boxes allowed
    var wrapper = $(".input_fields_wrap"); //Fields wrapper
    var add_button = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function{ //on add input button click
        if (x < max_fields) { //max input box allowed
            x++; //text box increment
            $.ajax({
                    url: '@Url.Action("AddDropdown", "YourController")',
                    type: "GET",
                    success: function (result) {
                        $(wrapper).append(result);
                    }
                });
        }
    });

    $(wrapper).on("click", ".remove_field", function (e) { //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

this Javascript means, when u click Add button, you will request to your controller using Ajax. Javascript意味着,当您单击“添加”按钮时,您将使用Ajax向您的控制器请求。 Then the controller will help you to render the dropdownlist and send the result back to your Client. 然后,控制器将帮助您呈现下拉列表并将结果发送回客户端。 So, you need to add another function in your controller to receive this Ajax request 因此,您需要在控制器中添加另一个功能以接收此Ajax请求

xxController xx控制器

public ActionResult AddDropdown()
{
    Model.Institute Result = new Model.Institute()
    //you can generate your Model.Institue in here

    Return PartialView("Institute", Result); //this will render Result with Institute.cshtml
}

after this, you can add dropdownlist into your html with the click add button 之后,您可以点击添加按钮将dropdownlist添加到html中

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

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