Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Quote:
Hi All,

I am working with a contact us page. Since the client have many branches, each page should be loaded with a unique design.

The page has enquiry (contact us)form part and a descriptive part. The form contains controls for getting details from the enquirer.

While submiting the form, the controls should be reset to its default values and the descriptive part should not change. I wrote the code for clearing the modelstate in controller. Is there any other way for clearing form controls only. Or shall I use partial View here for the enquiry(contact us) form. Please help me..

My code in Model
C#
public class CandidateContactUs
    {
        public CandidateContactUs()
        {
            statesList = new SelectList(new List<SelectListItem>(), "stateID", "state");
            provinceList = new SelectList(new List<SelectListItem>(), "provinceID", "province");
        }
        public SelectList statesList { get; set; }

        public SelectList provinceList { get; set; }

        private string _unCandidateFirstName;
        [Required(ErrorMessage = "Please type your name")]
        public string unCandidateFirstName { get { return _unCandidateFirstName; } set { _unCandidateFirstName = value; } }

        private string _unCandMobilePhone;
        [Required(ErrorMessage = "Please type your mobile number")]
        public string unCandMobilePhone { get { return _unCandMobilePhone; } set { _unCandMobilePhone = value; } }

        private int _countryID;
        [Display(Name = "Country")]
        public int countryID { get { return _countryID; } set { _countryID = value; } }
        
        private int _stateID;
        [Display(Name = "State")]
        public int stateID { get { return _stateID; } set { _stateID = value; } }
       
        private int _provinceID;
        public int provinceID { get { return _provinceID; } set { _provinceID = value; } }

                private tblBranch _clsBranch;
        public tblBranch clsBranch
        { get { return _clsBranch; } set{ _clsBranch = value;}}

In View
C#
@using (Html.BeginForm())
                    {
                        @Html.ValidationSummary(true);
                        <fieldset>
                            <div class="editor-label">
                                <label for="unCandidateFirstName">
                                    Name</label>
                            </div>
                            <div class="editor-field">
                                @Html.EditorFor(model => model.unCandidateFirstName)
                                @Html.ValidationMessageFor(model => model.unCandidateFirstName)
                            </div>
                            <div class="editor-label">
                                <label for="unCandMobilePhone">
                                    Mobile No</label>
                            </div>
                            <div class="editor-field">
                                @Html.EditorFor(model => model.unCandMobilePhone)
                                @Html.ValidationMessageFor(model => model.unCandMobilePhone)
                            </div>                            
                            <div class="editor-label">
                                <label for="countryID">
                                    Country</label>
                            </div>
                            <div class="editor-field">
                                @Html.DropDownListFor(model => model.countryID, new SelectList(countryList, "countryID", "country"), new { @id = "drpCountry", @class="input-large" })
                                @Html.ValidationMessageFor(model => model.countryID)
                            </div>
                            <div class="editor-label">
                                <label for="stateID">
                                    State</label>
                            </div>
                            <div class="editor-field">
                                @Html.DropDownListFor(model => model.stateID, Model.statesList, new { @id = "drpState" })
                            </div>
                            <div class="editor-label">
                                <label for="provinceID">
                                    Province</label>
                            </div>
                            <div class="editor-field">
                                @Html.DropDownListFor(model => model.provinceID, Model.provinceList, new { @id = "drpProvince" })
                            </div>
                            
                            <p>
                                <input type="submit" value="Contact Us" />
                            </p>
                        </fieldset>                                                
                    }
                </div>
                <div>
                    <h3>
      @Model.clsBranchDetails.tblBranchType.branchType</h3>
                            <ul class="contact-us">
                                <li>class="fa fa-map-marker">
                                        @Model.clsBranchDetails.branchAddress
                                        <br>
                                        @Model.clsBranchDetails.branchAddressPIN
                                    </p>
                                </li>
                                <li>^__i class="fa fa-phone">
                                    <p>
                                        ^__strong>Phone: @Model.clsBranch.tblBranchInCharges.Where(s => s.recordActive == true).Select(p => p.branchInChargePhone).FirstOrDefault();
                                    </p>
                                </li>
</ul>
</div>

In Controller
C#
[HttpPost]
        public ActionResult ContactUs(CandidateContactUs candidateContactUs)
        {
            SM_WEB.Models.SMWebDefaultConnection objSMWebDefaultConnection = new SMWebDefaultConnection();
                       objSMWebDefaultConnection.smConn.spUNCandidateContactUs(candidateContactUs.unCandidateFirstName, candidateContactUs.unCandMobilePhone, candidateContactUs.countryID, candidateContactUs.stateID, candidateContactUs.provinceID);
            objSMWebDefaultConnection.smConn.SaveChanges();

            ModelState.Clear();
            return View(candidateContactUs);
        }

Posted

1 solution

You could create a method from your CandidateContactUs class that reset and init your object with the default values, then to use this method in your controller in place of the ModelState.Clear(); in this way you will find more control about the default values that should be reset.
 
Share this answer
 
Comments
Jineesh TR 5-Nov-14 2:27am    
Can I make this as partial view. Is partial view fit for this condition.
Raul Iloc 5-Nov-14 2:36am    
1.Yes, you could use Partial View, if you want it, but the logic for resetting the default values should be in the controller even you will use Partial View.
2.Partial view make sense to be use, in your case, together with AJAX call, so only the dynamic part of your page to be sent to the server.
Jineesh TR 5-Nov-14 4:57am    
Thank you Raul Iloc.. It works fine for me..
Raul Iloc 5-Nov-14 6:17am    
Welcome, I am glad that I could help you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900