我正在设置一项功能,根据选择下拉菜单中的选择发送特定电子邮件。根据选择,需要发送特定的邮件。邮件是从作为处理程序的局部 View 发送的。
当页面加载时,每个局部 View 都会被渲染,所有邮件都会被发送,我不明白为什么这不起作用。
这是在 umbraco 中制作的,部分是从后台制作的,所以没有 Controller 可以渲染。我已经尝试过 switch 和 if 语句,但无法真正让它按照我想要的方式工作。
//My attempt at the function for the rendering of partials
function SendMail(){
var select = document.getElementById("foo");
var selected = select.options[select.selectedIndex].value;
if (selected == 1) {
$.ajax({ url: @Html.Partial("Web/Partial1", @Model.boo) })
}
if(selected == 2){
$.ajax({ url: @Html.Partial("Web/Partial2", @Model.boo)})
}
//switch (selected) {
// case '0':
// alert(selected);
// break;
// case '1':
// alert(selected);
// $.ajax({ url: Html.Partial("Web/Partial1", Model.boo)})
// break;
// case '2':
// alert(selected);
// $.ajax({ url: Html.Partial("Web/Partial2", Model.boo)})
//
//这是我选择的html。
<select id="foo" onchange="SendMail()">
<option value="0" selected="selected">Vælg en type</option>
<option value="1">Ordrebekræftigelse</option>
<option value="2">Betalingsmail</option>
</select>
最佳答案
实际上是Html附近的语法错误。部分,它不接受 Html.Partial("Web/Partial1", Model.boo) 作为 URL。(通过删除自行查看,它不会给出错误 SendMail Not defined)
所以我建议,在 Controller 中创建一个方法
public ActionResult CallPartial(int selected){
/// you can use switch case also
if(selected== 1)
return PartialView("yourpartial1",YourModel);
if(selected == 2)
return PartialView("yourpartial2",YourModel);
}
并在 ajax 响应中,将部分 View 附加到所需的 div
function SendMail(){
var select = document.getElementById("foo");
var selected = select.options[select.selectedIndex].value;
$.get( "YourControllerName/CallPartial",{selected : selected }, function(
data ) {
$( ".result" ).html( data );
});
}
<div class='result'></div>
关于javascript - Switch 语句触发所有 @html.partials 而不是选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56948295/