在服务器端,我有一个返回JsonResult的事务:
public JsonResult DoStuff(Guid id, string userInputText)
{
var product = _repository.Product(id); //busines logic
//Only a specific product must have userInputText <= 10 characters.
//Other products may have as many characters as the user wants.
if(product == Enum.SpecificProduct && userInputText.Count() > 10)
{
//The user input text comes from the View...
//If it has more then 10 characters, need to send the errorMessage to the View.
return Json(new { success = false, errorMessage = "error message" }, JsonRequestBehavior.AllowGet);
}
//Otherwise, do stuff on the product...
//and return success at the end.
return Json(new { success = true });
}
另一方面,在客户端,我有以下内容:
客户端:
using (Ajax.BeginForm("DoStuff", ajaxOptions))
{
<span>Enter the text:</span>
@Html.TextArea("userInputText", new { onkeyup = "SyncContents(); return false;" })
<input type="submit" value="Add" />
<!-- error message should be displayed here-->
}
这是AjaxOptions:
var ajaxOptions= new AjaxOptions
{
OnSuccess = "reload",
OnFailure = "FailMessage"
};
如果输入的文本超过10个字符,则在按下“添加”按钮时,控制器将在服务器端执行代码并失败,我该如何从那里获取errorMessage并在此处使用它,在视图中通知用户?
我尝试弹出消息:
<script>
function FailMessage() {
alert("Fail Post");
}
</script>
但是没有弹出“发布失败”的提示窗口。
最好的问候。