当满足特定条件时,如何使控制器产生javascript视觉效果?
我目前正在编写一个ASP.MVC应用程序。我差不多做完了。我现在做的外观和感觉的网站。问题是,我的视图中有一段javascript,我希望在满足特定条件时从控制器调用它 下面是代码示例 MenuController.cs:当满足特定条件时,如何使控制器产生javascript视觉效果?,javascript,jquery,asp.net-mvc,exception,html,Javascript,Jquery,Asp.net Mvc,Exception,Html,我目前正在编写一个ASP.MVC应用程序。我差不多做完了。我现在做的外观和感觉的网站。问题是,我的视图中有一段javascript,我希望在满足特定条件时从控制器调用它 下面是代码示例 MenuController.cs: [HttpPost] public ActionResult Create(FormCollection collection,Models.Menu sentData) { try { // TODO:
[HttpPost]
public ActionResult Create(FormCollection collection,Models.Menu sentData)
{
try
{
// TODO: Add insert logic here
//db.Menus.AddObject(sentData);
int existingQuery = (from m in db.Menus where m.Weekstart == Helper.GetNextMonday() && m.category == sentData.category select m).Count();
if (existingQuery > 0)
{
throw new Exception();
}
Models.Menu MenuItem = new Models.Menu
{
Monday = sentData.Monday,
Wednesday = sentData.Wednesday,
Friday = sentData.Friday,
category = sentData.category,
Weekstart = Helper.GetNextMonday(),
};
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
//Javascript visual here
return View();
}
}
下面是这个方法的创建视图
/菜单/Create.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Lunch_Ordering_Application.Models.Menu>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Create</h2>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<div class="editor-label">
<%: Html.LabelFor(model => model.Monday) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Monday) %>
<%: Html.ValidationMessageFor(model => model.Monday) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Wednesday) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Wednesday) %>
<%: Html.ValidationMessageFor(model => model.Wednesday) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Friday) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Friday) %>
<%: Html.ValidationMessageFor(model => model.Friday) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.category) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.category,new SelectList(new List<string> {"Nothing","Traditional","Chill Out","Healthy Lifestyle","Vegitarian","Salad"})) %>
<%: Html.ValidationMessageFor(model => model.category) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
<script src="/js/all.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
ReadyMade.init();
$('.growl_trigger').live('click', function (e) {e.preventDefault();
$.msgGrowl({type: error, title: "Error!", text: 'The category for this list is already listed for next week Monday! Please change the categories here.'});
});
</script>
<div class="msgGrowl-container bottom-right"></div>
</asp:Content>
创造
创造
型号(星期一)%>
型号(星期一)%>
型号(星期一)%>
型号(星期三)%>
型号(星期三)%>
型号(星期三)%>
型号(星期五)%>
型号(星期五)%>
型号(星期五)%>
型号(类别)%>
model.category,新选择列表(新列表{“无”、“传统”、“放松”、“健康生活方式”、“素食主义”、“沙拉”}))%>
型号(类别)%>
$(函数(){
ReadyMade.init();
$('.growl_trigger').live('click',函数(e){e.preventDefault();
$.msgGrowl({type:error,title:“error!”,text:“此列表的类别已在下周星期一列出!请在此处更改类别”。});
});
< JavaScript在页面的底部。如果在控制器中出现异常,我想尝试调用它。忽略实际的错误消息。这是“远程”可能的吗?还是有更简单的方法? < P>您可以考虑在catch语句中返回一个返回<代码> JavaScript(脚本)< /代码>,但是,不推荐。
<>你确实应该让你的客户做客户端事务。 你可以考虑在你的catch语句中返回一个返回的代码<代码> JavaScript(脚本)< /代码>,但是,不推荐。
你真的应该让你的客户做客户端业务。我已经为我的问题找到了一个个人的解决办法,它可以工作。当满足用户输入的特定条件时,可以调用Javascript 在您使用的控制器中,声明ViewData[“name here”]变量,并对其执行条件
public ActionResult Create()
{
ViewData["error"] = false;
ViewData["Heading"] = "Create a new menu Item";
return View();
}
[HttpPost]
public ActionResult Create(FormCollection collection,Models.Menu sentData)
{
try
{
// TODO: Add insert logic here
//db.Menus.AddObject(sentData);
ViewData["Heading"] = "Create a new menu Item";
DateTime MondaysDate = Helper.GetNextMonday();
bool existingQuery = ((from m in db.Menus where m.Weekstart == MondaysDate.Date && m.category == sentData.category select m).Count() > 1) ? true : false;
if (existingQuery)
{
ViewData["error"] = true;
return View();
}
else
{
Models.Menu MenuItem = new Models.Menu
{
Monday = sentData.Monday,
Wednesday = sentData.Wednesday,
Friday = sentData.Friday,
category = sentData.category,
Weekstart = Helper.GetNextMonday(),
};
db.Menus.AddObject(MenuItem);
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
//ViewData["error"] can also be set here
return View();
}
}
并且认为:
<script src="/js/all.js" type="text/javascript"></script>
<% if ((bool)ViewData["error"] == true)
{ %>
<script type="text/javascript"> $(function () {
ReadyMade.init();
$.msgGrowl({ type: 'error', title: 'Failed to insert record', text: 'Regretfully, your record can not be entered into the database, as menu items of this category is already booked for next Monday. Kindly change the category to something else.'
});
});</script>
<%} %>
$(函数(){
ReadyMade.init();
$.msgGrowl({type:'error',title:'insert record'Failed',text:'很遗憾,您的记录无法输入数据库,因为该类别的菜单项已预订到下周一。请将该类别更改为其他类别。'
});
});
在我看来,当满足特定条件时,这是一个非常有效的解决方法,可以从这一点运行一段javascript/jquery。我已经为我的问题找到了一个个人解决方法,它可以工作。当满足用户输入的特定条件时,可以调用javascript 在您使用的控制器中,声明ViewData[“name here”]变量,并对其执行条件
public ActionResult Create()
{
ViewData["error"] = false;
ViewData["Heading"] = "Create a new menu Item";
return View();
}
[HttpPost]
public ActionResult Create(FormCollection collection,Models.Menu sentData)
{
try
{
// TODO: Add insert logic here
//db.Menus.AddObject(sentData);
ViewData["Heading"] = "Create a new menu Item";
DateTime MondaysDate = Helper.GetNextMonday();
bool existingQuery = ((from m in db.Menus where m.Weekstart == MondaysDate.Date && m.category == sentData.category select m).Count() > 1) ? true : false;
if (existingQuery)
{
ViewData["error"] = true;
return View();
}
else
{
Models.Menu MenuItem = new Models.Menu
{
Monday = sentData.Monday,
Wednesday = sentData.Wednesday,
Friday = sentData.Friday,
category = sentData.category,
Weekstart = Helper.GetNextMonday(),
};
db.Menus.AddObject(MenuItem);
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
//ViewData["error"] can also be set here
return View();
}
}
并且认为:
<script src="/js/all.js" type="text/javascript"></script>
<% if ((bool)ViewData["error"] == true)
{ %>
<script type="text/javascript"> $(function () {
ReadyMade.init();
$.msgGrowl({ type: 'error', title: 'Failed to insert record', text: 'Regretfully, your record can not be entered into the database, as menu items of this category is already booked for next Monday. Kindly change the category to something else.'
});
});</script>
<%} %>
$(函数(){
ReadyMade.init();
$.msgGrowl({type:'error',title:'insert record'Failed',text:'很遗憾,您的记录无法输入数据库,因为该类别的菜单项已预订到下周一。请将该类别更改为其他类别。'
});
});
在我看来,当满足特定条件时,这是一个非常有效的解决方法,可以从这一点运行javascript/jquery。1小时,我没有找到好的解决方案。谢谢。标记答案。1小时,我没有找到好的解决方案。谢谢。标记答案。