kows
kows

Reputation: 121

MVC Remote Attribute - Issue with submit

I'm trying to check if the combination of typeId & supplementId already exists in my database. So I've added a remoteattribute to my supplementId which does this. My problem is that the validation should trigger on changing either dropdownlist. I kinda fixed this clientside with the javascript on the ddl onchange. But now I'm not able to post my form. When I click the button, it focuses the last dropdownlist but doesn't show any errors and my Post method in the controller doesn't get fired. If I execute a form.submit() in chrome console the method gets called but validation is ignored. Anyone had this issue before?

BTW this code is cleaned to a minimum so labels, errormessages, etc could be missing, these shouldn't be the issue.

Partial view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SupplementTypeClass>" %>

<script src="<%: Url.Content("~/Scripts/jquery-1.5.1.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>"
    type="text/javascript"></script>
<% using (Html.BeginForm("AddEditSupplementType", "Supplement"))
   { %>
<%: Html.ValidationSummary(true)%>
<fieldset>

    <legend>
        Add type
    </legend>
    <div class="editor-content">
        <div class="editor-label">
            <%: Html.LabelFor(model => model.SupplementId)%>
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.SupplementId, new SelectList(ViewBag.Supplements, "SupplementId", "Name", Model.SupplementId), "Select supplement", new { onchange = "$('#ddl').removeData('previousValue');$('#ddl').valid();" })%>
            <%: Html.ValidationMessageFor(model => model.SupplementId)%>
        </div>
    </div>
    <div class="editor-content">
        <div class="editor-label">
            <%: Html.LabelFor(model => model.TypeId)%>
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.TypeId, new SelectList(ViewBag.Types, "TypeId", "Name", Model.TypeId), "Select type", new { id ="ddl" })%>
            <%: Html.ValidationMessageFor(model => model.TypeId)%>
        </div>
    </div>
    <div class="editor-buttons">
        <input type="submit" value="Save" />
    </div>
</fieldset>
<% } %>

Controller:

[HttpPost]
        public ActionResult AddEditSupplementType(SupplementTypeClass sup)
        {
            if (ModelState.IsValid)
            {
                //code to insert to DB
            }
            return RedirectToAction("FicheAddEditSupplementType", new { supplementTypeId = sup.supplementTypeId});
        }

Class:

public class SupplementTypeClass
{

    #region Members

    public int SupplementId { get; set; }

    [RemoteAttribute("SupplementTypeCheck", "Supplement", AdditionalFields = "SupplementId")]
    public int TypeId { get; set; }

    #endregion
}

Upvotes: 1

Views: 808

Answers (1)

Pluc
Pluc

Reputation: 2929

The Remote attribute does Ajax calls using the GET method by default, not POST.

Make your you specificy the POST method in your model

[RemoteAttribute("SupplementTypeCheck", "Supplement", AdditionalFields = "SupplementId", HttpMethod="POST")]

Upvotes: 1

Related Questions