서버 측에 나는 JsonResult 반환 트랜잭션이 다음 클라이언트 측에 내가 가지고, 다른 한편으로컨트롤러가 실패하면보기에 오류를 표시하는 방법? ASP.NET MVC
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 });
}
을이 :
var ajaxOptions= new AjaxOptions
{
OnSuccess = "reload",
OnFailure = "FailMessage"
};
:
이
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입니다
입력 한 텍스트의 글자 수가 10 자 이상인 경우 "추가"버튼을 누르면 컨트롤러가 서버 쪽에있는 코드를 실행하고있어 오류가 발생합니다. 어떻게 errorMessage를 가져 와서 여기에서 사용할 수 있습니까? 보기에서 사용자에게 알리기 위해?
내가 메시지를 알리기 위해 노력 :<script>
function FailMessage() {
alert("Fail Post");
}
</script>
하지만 팝업을 "실패 포스트"가 나타납니다.
감사합니다.
위 코드에서 ajaxOptions가 saveOptions와 어떻게 연결되어 있는지 확실하지 않습니다. 어쩌면 새로운 Ajaxoptions {OnSuccess = "reload", OnFailure = "FailMessage"}로 바꾸고, "reload"다음에 ","를 추가하는 것을 잊지 마십시오. – oopsdazie
정말 그 실수로 유감스럽게 생각합니다. saveOptions 실제로 ajaxOption, 나는 잘못된 이름을 대처했다. 고맙습니다. –
그래서 사용자는 텍스트 영역에 최대 10 자까지 입력 할 수 있습니다. 사용자가 11 자 이상을 입력하면 오류 메시지가 나타납니다. –