So I'm trying to implement RemoteAttribute
, but when I click the submit
button, it's not giving post
to 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")
}