Can not submit with Remote Validation

My remote validation is working, but when I submit the form the cursor focus on the valid field, and no error message.

This my code:

ProjectModel:

    [Required]
    [Remote("ProjectNameVerify", "Projects")]
    public string Name { get; set; }

ProjectsController:

    public ActionResult ProjectNameVerify(string name)
    {
       // ... 
        return Json("msg", JsonRequestBehavior.AllowGet);
    }

Project.cshtml:

@using (Html.BeginForm())
@Html.AntiForgeryToken()
<div class="form-horizontal">
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", Autofocus = "false" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
        <br>
    </div>
    <br>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-success" />
        </div>
    </div>
</div>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Best practice for me:

ProjectModel:

[Required]
[Remote("ProjectNameVerify", "Projects")]
public string Name { get; set; }

ProjectsController:

    public JsonResult ProjectNameVerify(string name)
    {
        if (Verify() == false)
        {
            return Json("errormsg", JsonRequestBehavior.AllowGet);
        }

        return Json(true, JsonRequestBehavior.AllowGet);
    }

Method 2

Change ProjectNameVerify controller to this. False, then error message shown, true then you can submit the form.

public JsonResult ProjectNameVerify(string name)
{
   // ... 
    return Json({true/false}, JsonRequestBehavior.AllowGet);
}

Reference: Remote Validation In MVC 5 Using Remote Attribute


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x