简体   繁体   中英

ASP.NET razor: Form not submitting

Since I added a DisplayFormat and datepicker to my DateTime properties/inputfields, my form stopped submitting. I don't get any errors (In chrome F12 or Visual studio).

Edit: I've set Displayformat to dd/MM/yyyy and the datepicker to "dd/mm/yyyy". But that didn't solve it either.

@using (Html.BeginForm("CreateCampaign", "Home")) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Campaign</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Campaign.CampaignName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Campaign.CampaignName)
            @Html.ValidationMessageFor(model => model.Campaign.CampaignName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Campaign.WebsiteUrl)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Campaign.WebsiteUrl)
            @Html.ValidationMessageFor(model => model.Campaign.WebsiteUrl)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Campaign.PrivacyPolicyUrl)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Campaign.PrivacyPolicyUrl)
            @Html.ValidationMessageFor(model => model.Campaign.PrivacyPolicyUrl)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Campaign.TermsUrl)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Campaign.TermsUrl)
            @Html.ValidationMessageFor(model => model.Campaign.TermsUrl)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Campaign.PricepageUrl)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Campaign.PricepageUrl)
            @Html.ValidationMessageFor(model => model.Campaign.PricepageUrl)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Campaign.Startdate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Campaign.Startdate)
            @Html.ValidationMessageFor(model => model.Campaign.Startdate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Campaign.Enddate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Campaign.Enddate)
            @Html.ValidationMessageFor(model => model.Campaign.Enddate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Campaign.Starthour)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.Campaign.Starthour, new SelectList(Model.Hours))
            @Html.ValidationMessageFor(model => model.Campaign.Starthour)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Campaign.Endhour)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.Campaign.Endhour, new SelectList(Model.Hours))
            @Html.ValidationMessageFor(model => model.Campaign.Endhour)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Campaign.PMAM)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.Campaign.PMAM, new SelectList(Model.AMPM, "key", "value"))
            @Html.ValidationMessageFor(model => model.Campaign.PMAM)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Campaign.Language)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.Campaign.Language, new SelectList(Model.Languages, "key", "value"))
            @Html.ValidationMessageFor(model => model.Campaign.Language)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Campaign.FK_ID_MerchantApp)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Campaign.FK_ID_MerchantApp)
            @Html.ValidationMessageFor(model => model.Campaign.FK_ID_MerchantApp)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Campaign.CampaignType)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.Campaign.CampaignType, new SelectList(Model.Types, "key", "value"))
            @Html.ValidationMessageFor(model => model.Campaign.CampaignType)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<script type="text/javascript">
    $('#Campaign_Startdate').datepicker({
        dateFormat: "DD, d MM, yy",
        minDate: new Date()
    });
    $('#Campaign_Enddate').datepicker({
        dateFormat: "DD, d MM, yy",
        minDate: new Date()
    });

</script>
<div>
    @Html.ActionLink("Back to List", "Index")
</div>




       public class Campaign
        {
            #region CTor
            public Campaign()
            {
            }
            #endregion

            #region Properties

            [XmlElement(ElementName = "Id_campaign")]
            public string ID_Campaign { get; set; }
            [XmlElement(ElementName = "Campaignname")]
            public string CampaignName { get; set; }
            [XmlElement(ElementName = "Websiteurl")]
            public string WebsiteUrl { get; set; }
            [XmlElement(ElementName = "Privacypolicyurl")]
            public string PrivacyPolicyUrl { get; set; }
            [XmlElement(ElementName = "Termsurl")]
            public string TermsUrl { get; set; }
            [XmlElement(ElementName = "Pricepageurl")]
            public string PricepageUrl { get; set; }
            [XmlElement(ElementName = "Maxcredit")]
            public Int32 MaxCredit { get; set; }
            [XmlElement(ElementName = "Fk_id_currency")]
            public string FK_ID_Currency { get; set; }
            [XmlElement(ElementName = "Maxscans")]
            public short MaxScans { get; set; }
            [XmlElement(ElementName = "Startdate")]
            [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:ddd, MMM d, yyyy}")]
            public DateTime Startdate { get; set; }
            [XmlElement(ElementName = "Enddate")]
            [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:ddd, MMM d, yyyy}")]
            public DateTime Enddate { get; set; }
            [XmlElement(ElementName = "Starthour")]
            public short Starthour { get; set; }
            [XmlElement(ElementName = "Endhour")]
            public short Endhour { get; set; }
            [XmlElement(ElementName = "Pmam")]
            public string PMAM { get; set; }
            [XmlElement(ElementName = "Language")]
            public string Language { get; set; }
            [XmlElement(ElementName = "Fk_id_merchantapp")]
            public string FK_ID_MerchantApp { get; set; }
            [XmlElement(ElementName = "Campaigntype")]
            public string CampaignType { get; set; }
            [XmlElement(ElementName = "Createtimestamp")]
            [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:ddd, MMM d, yyyy}")]
            public DateTime CreateTimestamp { get; set; }
            [XmlElement(ElementName = "Lastupdate")]
            [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:ddd, MMM d, yyyy}")]
            public DateTime LastUpdate { get; set; }
            [XmlElement(ElementName = "Lastupdateby")]
            public string LastUpdateBy { get; set; }
            [XmlElement(ElementName = "Status")]
            public short Status { get; set; }
    }

I see that you have validation summary set to true. Could it be that unobtrusive validation is preventing your form from being submitted and you cant see it because the error isn't attached to a specific field? Maybe, try to set it to false, and see if anything pops up? Do you have unobtrusive validation turned on along with client validation?

Did you use chrome's network tool to check if any http post's are being sent out upon submission? Have you tried using the "dd/mm/yyyy" in your model as well?

You don't have a form. Wrap it in Html.BeginForm:

 @{ using (Html.BeginForm()) { 




              ....




    }
 }

It might sound too obvious, but since it stopped working after you added the 'datepicker', and you are using jQuery, you should try adding

$(document).ready(function() {

});

to wrap your 'datepicker' code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM