Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • André Nøbbe Christiansen 10 posts 71 karma points
    Feb 24, 2015 @ 19:48
    André Nøbbe Christiansen
    0

    Page dosen't remember selected values from Checkboxlist on postback. MVC form

    Hello! I’m having trouble with a contact form where I want a CheckBoxList. I'm using the mvccbl extencion [http://mvccbl.com/].

    I want the user to check at least one from the list. When you do that, but doesn't fill in the other fields and submit, the server validates the form. Since the name and email is required the form returns invalid. But now the page doesn't remember the one which is selected from the list.
    Also the unobtrusive validation dosent kick in at the cbl???

    This can be seen on www4.etlgroup.dk/privat/kontakt-os/

    So far I have:

    Omraade.cs

    public class Omraade {

            public int Id { get; set; }

            public string Name { get; set; }

            public object Tags { get; set; }

            public bool IsSelected { get; set; }

        }

    ContactModel.cs

    public class ContactModel{

            public IList<Omraade> AvailableOmraader { get; set; }

            public IList<Omraade> SelectedOmraader { get; set; }

            [BooleanRequired(ErrorMessage = "Du skal vælge mindst et område.")] public PostedOmraader PostedOmraader { get; set; }

            [Required(ErrorMessage = "Dette felt er påkrævet.")] [Display(Name = "Navn")] public string Name { get; set; }

            [Required(ErrorMessage = "Dette felt er påkrævet.")] [RegularExpression(@"^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$", ErrorMessage = "Indtast en gyldig email-adresse.")] [Display(Name = "E-mail")] public string Email { get; set; }

            [Display(Name = "Telefonnummer")] public string Phone { get; set; }

            [Display(Name = "Evt. kommentar")] public string Message { get; set; }

            public IHtmlString SuccesMsg { get; set; }

            public IHtmlString ErrorMsg { get; set; }

        }

        //Helper Class to make posting back selected values easier public class PostedOmraader{

    public string[] OmraaderIDs { get; set; }

        }

    }

    ContactSurfaceController.cs

    public class ContactSurfaceController : SurfaceController {

            public ActionResult RenderContactForm(){

                var omraader = Umbraco.Field("omraader").ToString().Split(',');

                ListOmraade> OmraaderList = new ListOmraade>();

                for (int i = 0; i <= omraader.Length - 1; i++){

                    OmraaderList.Add(new MvcEltGroup.Logic.Omraade { Name = omraader[i], Id = i });

                }

                return PartialView("/Views/Partials/Forms/ContactForm.cshtml", new ContactModel {  

                    AvailableOmraader = OmraaderList,

                    ErrorMsg = Umbraco.Field("errorMsg"),

                    SuccesMsg = Umbraco.Field("successMsg")

                });

            }

     

            [HttpPost] [ValidateAntiForgeryToken] public ActionResult HandleContactForm(ContactModel model) {

                if (!ModelState.IsValid){

                    return CurrentUmbracoPage();

                }

                try {

                    StringBuilder sb = new StringBuilder();

                    sb.AppendFormat("Name: {0}", model.Name);

                    sb.AppendFormat("Email: {0}", model.Email);

                    sb.AppendFormat("Phone: {0}", model.Phone);

                    sb.AppendFormat("{0}", model.Message);

                    library.SendMail(model.Email, "[email protected]", "Kontakt hjemmesiden", sb.ToString(), true);

                    TempData["IsSuccessful"] = true;

                }catch (Exception ex){

                    TempData["IsError"] = ex.ToString();

                }

                return CurrentUmbracoPage();

            }

        }

    Contact.cshtml

    @Html.Action("RenderContactForm", "ContactSurface")

    ContactForm.cshtml

    using (Html.BeginUmbracoForm<ContactSurfaceController>("handleContactForm")){

            @Html.ValidationSummary(true)

            @Html.AntiForgeryToken()

            var htmlListInfo = new HtmlListInfo(HtmlTag.table, 2, new { @class = "checkboxlist-table" }, TextLayout.Default, TemplateIsUsed.No);

            <div class="omraader form-group">

                <h5>Hvad ønsker du at blive kontaktet angående? <span class="f_required">*>>

                @Html.CheckBoxListFor( x => x.PostedOmraader.OmraaderIDs, x => x.AvailableOmraader, x => x.Id, x => x.Name, x => x.SelectedOmraader, htmlListInfo, x => new { @class="cblOmraade checkbox" } )

                @Html.ValidationMessageFor(x => x.PostedOmraader)

            >

            <hr>

            <div class="form-group">

                @Html.LabelFor(x => x.Name) <span class="f_required">*>

                @Html.ValidationMessageFor(x => x.Name)

                @Html.TextBoxFor(x => x.Name)

            >

     

            <div class="row">

                <div class="col-sm-8">

                    <div class="form-group">

                        @Html.LabelFor(x => x.Email) <span class="f_required">*

                        @Html.ValidationMessageFor(x => x.Email)

                        @Html.TextBoxFor(x => x.Email)

                    >

                >

                <div class="col-sm-4">

                    <div class="form-group">

                        @Html.LabelFor(x => x.Phone)

                        @Html.ValidationMessageFor(x => x.Phone)

                        @Html.TextBoxFor(x => x.Phone)

                    >

                >

            >


            <div class="form-group">

                @Html.LabelFor(x => x.Message)

                @Html.ValidationMessageFor(x => x.Message)

                @Html.TextAreaFor(x => x.Message, new { @rows="8", @cols="48" })

            >


            <input type="submit" value="Send" />

        }

    I hope you can help me. And thanks in advance :)

Please Sign in or register to post replies

Write your reply to:

Draft