我开始玩弄 mvc。
我需要以下内容:一个组合框,当用户选择其中一个项目时,将弹出一个消息框并显示“这次请工作!!!”。
这是我的代码:
索引.html:
@Html.DropDownListFor(x => x.SelectedFileName, Model.Files, new { Name = "map", @class = "form-control" })
家庭虚拟机:
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 不会弹出。
你知道我缺少什么吗?
你不能MessageBox.Show
在 asp.net MVC 它是为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
直接在 dropdownList 中绑定事件。
@Html.DropDownListFor(x => x.SelectedFileName, Model.Files, new { Name = "map", @class = "form-control" , onchange = @"alert('Please work this time!!!')"})
编辑
这是一个带有asp.net小样本的ajax。
在Razor
页面中包含 jQuerylib (html 页面)
<script
src="https://code.jquery.com/jquery-2.2.4.js"
integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI="
crossorigin="anonymous"></script>
下拉列表中的绑定Onchange
事件id='SelectedFileName'
。
使用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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句