Asp.Net Mvc Remote Validation

Asked

Viewed 345 times

0

So I’m trying to implement RemoteAttribute, but by clicking the button submit, is not giving the post on my page.

My Controller

public class TesteRemoteController : Controller
{
    // GET: TesteRemote
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Create()
    {
        return View(new TesteRemoteViewModel());
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Nome,Observacao")] TesteRemoteViewModel model)
    {

        if (ModelState.IsValid)
        {
            //salvar no banco

            return RedirectToAction("Create");
        }

        return View(model);
    }

    public JsonResult IsNomeAvailable(string nome, int Id)
    {
        //verificar no banco se o nome não é duplicado
        if (true)
            return Json(true, JsonRequestBehavior.AllowGet);

        string suggestedUID = String.Format(CultureInfo.InvariantCulture,
            "O upload {0} já foi cadastrado.", nome);

        return Json(suggestedUID, JsonRequestBehavior.AllowGet);
    }
}

My Model

public class TesteRemoteViewModel
    {
        public int Id { get; set; }

        [Required]
        [StringLength(100, MinimumLength = 3)]
        [System.Web.Mvc.Remote("IsNomeAvailable", "TesteRemote", AdditionalFields = "Id")]
        public string Nome { get; set; }

        [Required]
        [StringLength(100, MinimumLength = 3)]
        public string Observacao { get; set; }
    }

My View

    @model WebApplication5.Models.TesteRemoteViewModel

@{
    ViewBag.Title = "TesteRemote";
}

<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                TesteRemote
            </div>
            <div class="panel-body">
                @using (Html.BeginForm())
                {

                    @Html.AntiForgeryToken()

                    <div class="form-group">
                        @Html.ValidationSummary(false, "", new { @class = "text-danger" })

                        @Html.HiddenFor(a=>a.Id)

                        <div class="row form-group">
                            <div class="col-lg-6">
                                @Html.LabelFor(model => model.Nome)
                                @Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <div class="row form-group">
                            <div class="col-lg-6">
                                @Html.LabelFor(model => model.Observacao)
                                @Html.EditorFor(model => model.Observacao, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.Observacao, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <div class="form-group">
                            <input type="submit" value="Salvar" class="btn btn-default" />
                            <a href="@Url.Action("Index")" class="btn btn-default">Voltar</a>
                        </div>
                    </div>


                }
            </div>
        </div>
    </div>
</div>

@section scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
  • So, I believe that my main problem was giving Post twice with the same data filled in the field Name using IE, because it has the Cache, to solve just added the [OutputCache(Location = System.Web.UI.OutputCacheLocation.None)] in my method that I use in RemoteAttribute.

  • Another very important thing is the method Create() already return an instance to Model.

1 answer

0


The solution was in Action Create return an instance of the model

public ActionResult Create()
{
    return View(new TesteRemoteViewModel());
}

And in the method called by RemoteAttribute disable the Cache using the attribute OutputCache

[OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]
public JsonResult IsNomeAvailable(string nome, int Id)
{
    //verificar no banco se o nome não é duplicado
    if (true)
        return Json(true, JsonRequestBehavior.AllowGet);
    string suggestedUID = String.Format(CultureInfo.InvariantCulture,
                    "O upload {0} já foi cadastrado.", nome);

    return Json(suggestedUID, JsonRequestBehavior.AllowGet);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.