如何在我的MVC4“联系我们”页面中有效地使用部分视图? [英] How do I use Partial View effectively in my MVC4 'contact us' page?

查看:73
本文介绍了如何在我的MVC4“联系我们”页面中有效地使用部分视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

@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">
                                    <p>
                                        class="contact-pad">Address:
                                        @Model.clsBranchDetails.branchAddress
                                        <br>
                                        @Model.clsBranchDetails.branchAddressPIN
                                    </p>
                                </li>
                                <li>class="fa fa-phone">
                                    <p>
                                        Phone: @Model.clsBranch.tblBranchInCharges.Where(s => s.recordActive == true).Select(p => p.branchInChargePhone).FirstOrDefault();
                                    </p>
                                </li>
</ul>
</div>



在控制器中


In Controller

[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);
        }




推荐答案

你可以用你的<$创建一个方法c $ c> CandidateContactUs 使用默认值重置和初始化对象的类,然后在控制器中使用此方法代替 ModelState.Clear();通过这种方式,您可以找到有关应重置的默认值的更多控制权。
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.


这篇关于如何在我的MVC4“联系我们”页面中有效地使用部分视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆