为什么我不能在div内容中放置DropDownListFor?

时间:2022-07-01 21:05:53

I have two DropDownListFor's, and the content of the second depends on the first.

我有两个DropDownListFor,第二个的内容取决于第一个。

I'm attempting to refresh the div content (list) every time a different selection is made in the first with a bit of javascript:

我正在尝试刷新div内容(列表)每次在第一次使用一些javascript进行不同的选择时:

function onUpdate() {
    $("#MyDiv").html('<%= Html.DropDownListFor(model => model.PeriodSelected, Model.PeriodSelectList) %>');
}

And the div is simply:

div简单地说:

<div id="MyDiv" class="editor-field" />

The SelectList in the ViewModel depends on the selected item from the first list:

ViewModel中的SelectList取决于第一个列表中的选定项:

public SelectList PeriodSelectList
    {
        get
        {
            var list = this.Versions.ToList().Where(fv => fv.DateSubmitted.Year == PeriodSelected);
            return new SelectList(list, "Id", "Version");
        }
    }

Unfortunately running this results in:

不幸的是,运行此结果:

Microsoft JScript runtime error: Could not complete the operation due to error 80020101.

Microsoft JScript运行时错误:由于错误80020101无法完成操作。

However, I've tried using:

但是,我尝试过使用:

$("#MyDiv").html('<%= Html.LabelFor(...) %>');

for the onUpdate() function and that works fine, so the problem seems to be with the DropDownListFor.

对于onUpdate()函数而且工作正常,所以问题似乎与DropDownListFor有关。

So why is this not working? Thanks

那为什么这不起作用?谢谢

1 个解决方案

#1


0  

I'm not sure about error, afaik it's something about internet explorer and parsing errors, but.

我不确定错误,afaik它是关于Internet Explorer和解析错误的东西,但是。

Dropdown list markup actually generates only once on response, so this string will be constant.

下拉列表标记实际上只在响应时生成一次,因此该字符串将是常量。

<%= Html.DropDownListFor(model => model.PeriodSelected, Model.PeriodSelectList) %> will be the same <select>...</select> all the time.

<%= Html.DropDownListFor(model => model.PeriodSelected,Model.PeriodSelectList)%>将始终与

You need to rethink workflow of your web site to preload all needed information and then work with that only by JavaScript. Thus, you need to filter your periods in browser with javascript and left asp.net mvc only as data source.

您需要重新考虑网站的工作流程以预加载所有需要的信息,然后仅使用JavaScript进行处理。因此,您需要使用javascript在浏览器中过滤期间,并将asp.net mvc仅作为数据源。

Or somehow use ajax to regenerate dropdown list markup on server. In simple words, in ASP.NET MVC you cannot easily mix JavaScript and .NET code. And I think, this is right approach. But, if you want so for some reason:

或者以某种方式使用ajax重新生成服务器上的下拉列表标记。简单来说,在ASP.NET MVC中,您无法轻松混合JavaScript和.NET代码。我认为,这是正确的方法。但是,如果您出于某种原因需要:

there're WebForms.

#1


0  

I'm not sure about error, afaik it's something about internet explorer and parsing errors, but.

我不确定错误,afaik它是关于Internet Explorer和解析错误的东西,但是。

Dropdown list markup actually generates only once on response, so this string will be constant.

下拉列表标记实际上只在响应时生成一次,因此该字符串将是常量。

<%= Html.DropDownListFor(model => model.PeriodSelected, Model.PeriodSelectList) %> will be the same <select>...</select> all the time.

<%= Html.DropDownListFor(model => model.PeriodSelected,Model.PeriodSelectList)%>将始终与

You need to rethink workflow of your web site to preload all needed information and then work with that only by JavaScript. Thus, you need to filter your periods in browser with javascript and left asp.net mvc only as data source.

您需要重新考虑网站的工作流程以预加载所有需要的信息,然后仅使用JavaScript进行处理。因此,您需要使用javascript在浏览器中过滤期间,并将asp.net mvc仅作为数据源。

Or somehow use ajax to regenerate dropdown list markup on server. In simple words, in ASP.NET MVC you cannot easily mix JavaScript and .NET code. And I think, this is right approach. But, if you want so for some reason:

或者以某种方式使用ajax重新生成服务器上的下拉列表标记。简单来说,在ASP.NET MVC中,您无法轻松混合JavaScript和.NET代码。我认为,这是正确的方法。但是,如果您出于某种原因需要:

there're WebForms.