从组合框中选择项目时显示MessageBox



我开始尝试mvc。

我需要以下内容:一个组合框,当用户选择其中一个项目时,会弹出一个messageBox,并显示"请这次工作!!"。

这是我的代码:

Index.html:

@Html.DropDownListFor(x => x.SelectedFileName, Model.Files, new { Name = "map", @class = "form-control" })

HomeVM:

public class HomeVM
{
public List<SelectListItem> Files { get; set; }
public string SelectedFileName { get; internal set; }
public List<string> DynamicAlgorithems { get; set; }
}

家庭控制器:

[HttpPost]
public ActionResult ShowAllMobileDetails(HomeVM MV)
{
string SelectedValue = MV.Files.Count.ToString();
MessageBox.Show("Please work this time!!!");
return View(MV);        
}

问题是:我可以在组合框中看到所有项目,但当用户选择其中一个时,MessageBox不会弹出。

你认出我缺了什么吗?

您不能在asp.net MVC中使用MessageBox.Show,它适用于winform

设置id='SelectedFileName'OnChange事件。并使用jsalert函数

你可以用js来做。

这是一个jQuery示例。

$('#SelectedFileName').change(function(){
alert('Please work this time!!!');
});

$('#SelectedFileName').change(function(){
alert('Please work this time!!!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select id="SelectedFileName">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

或者在您的问题中,您可以直接在下拉列表中绑定onchange事件。

@Html.DropDownListFor(x => x.SelectedFileName, Model.Files, new { Name = "map", @class = "form-control" , onchange = @"alert('Please work this time!!!')"})

编辑

下面是一个带有asp.net的ajax小示例。

  1. Razor页面(html页面(中包含jQuerylib

    <script
    src="https://code.jquery.com/jquery-2.2.4.js"
    integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI="
    crossorigin="anonymous"></script>
    
  2. 正在绑定id='SelectedFileName'下拉列表中的Onchange事件。

  3. 使用post方法进行ajax调用,使用第三个参数回调函数得到MVC结果。

看起来像这样,您可以在ShowAllMobileDetails操作中执行一些逻辑。

$('#SelectedFileName').change(function(){
//get id="SelectedFileName"
$.post('Home/ShowAllMobileDetails', $("form").serialize(),function(data){
alert(data);
});
});
[HttpPost]
public ActionResult ShowAllMobileDetails(HomeVM MV)
{
string SelectedValue = MV.Files.Count.ToString();
// your logic
string result = "Please work this time!!!";
return View(result);        
}

最新更新