The scenario is, I have a form to sign in user, which will invoke an action method. The action method will return true/false depending on valid/invalid username. If it returns false, I want a jquery method to show a message. I can pass the value as a model view, but problem is when the page is first time requested, that value is null because action method for post is not invoked that time, therefore its showing an error like object reference is null. How to handle this kind of situation. I’m not giving the code, coz coding is not my problem, I need to know the best practice to handle this. Thanks.
OK, Here is the Jquery method I tried.
<script type="text/javascript">
$(document).ready(function () {
(' #SignIn').click(function () {
if (@Model.test === null) //Error is showing here, on the @Model.test call
{
$(' #Message').text("Null")
}
if(@Model.test===false)
{
$(' #Message').text("Invalid");
}
});
});
</script>
if your are returning your result from action in model then you need to perform java-script operation after the POST call.
Try this.
i have give you solution according to your situation but i would suggest you submit form using Ajax.BeginForm or javascript and return JSON from action.