在Html.Beginform

时间:2016-05-11 07:12:27

标签: c# jquery asp.net-mvc forms

我已经在里面开始形式和手风琴。

看起来像这样:

enter image description here

可能有超过1个教育,工作经验等。

我的观点如下:

    @using (Html.BeginForm("Index", "CV", FormMethod.Post))
    {
        <div class="main">
            <div class="accordion">
                <div class="accordion-section private-information">
                    <a class="accordion-section-title main-color" href="#accordion-1">Private Information</a>
                    <div class="arrow-down"></div>
                    <div id="accordion-1" class="accordion-section-content">
                        <div class="private-information-divs">
                            <div class="pcontent-left main-color">name, surname</div>
                            @Html.TextBoxFor(m => m.privateInformation.Name, new { @class = "pcontent-right main-color" })
                        </div>
.................
                    </div><!--end .accordion-section-content-->
                </div><!--end .accordion-section-->


                <div class="accordion-section">
                    <a class="accordion-section-title main-color" href="#accordion-2">Education</a>
                    <div class="arrow-down"></div>
                    <div id="accordion-2" class="accordion-section-content">

                        @using (Ajax.BeginForm("EducationTable", new AjaxOptions()
                        {
                            InsertionMode = InsertionMode.Replace,
                            UpdateTargetId = "tblEducation"
                        }))
                        {
                            <div class="private-information-divs">
                                <div class="pcontent-left main-color">University</div>
                                @Html.TextBoxFor(m => m.newEducation.UniversityName, new { @class = "pcontent-right main-color" })
                                @Html.ValidationMessageFor(m => m.newEducation.UniversityName)
                            </div>
                            .................

                            <div class="add-education">
                                <button type="submit" class="add-education-button">add</button>
                            </div>
                        }

                        <div id="tblEducation">
                            @Html.Partial("EducationTable")
                        </div>
                    </div><!--end .accordion-section-content-->
                </div><!--end .accordion-section-->



            <input type="submit" class="continue" value="Continue" />
        </div>
    }

我的问题是嵌套表单。正如我在google中发现的那样,我无法嵌套表单( Html.Beginform 中的 Ajax.Beginform )。但我需要这样做,因为我正在构建我的模型。我的控制器是:

    [HttpPost]
    public ActionResult EducationTable(CV cv)
    {

        CV _cv = new CV();

        if (Session["makeCV"] != null)
            _cv = Session["makeCV"] as CV;
        _cv.education = _cv.education ?? new List<Education>();
        _cv.education.Add(new Education()
        {
            Id = Guid.NewGuid(),
            UniversityName = cv.newEducation.UniversityName,
            Faculty = cv.newEducation.Faculty,
            Specialization = cv.newEducation.Specialization,
            StartDate = cv.newEducation.StartDate,
            EndDate = cv.newEducation.EndDate,
            OnGoing = cv.newEducation.OnGoing
        });
        Session["makeCV"] = _cv;

        return PartialView("EducationTable", _cv);
    }

等。在索引行动中我收集整个模型

    [HttpPost]
    public ActionResult Index(CV model)
    {
        CV _cv = new CV();

        if (Session["makeCV"] != null)
            _cv = Session["makeCV"] as CV;
        .....
        return View();
    }

但是当我点击提交教育表单内的按钮(即Ajax.Beginform)时,它会提交整个表单(Html.Beginform)并进行索引操作。 我该如何解决这个问题?我需要浏览这些部分视图,然后提交整个表单

1 个答案:

答案 0 :(得分:0)

@using(Ajax.BeginForm("Search",new AjaxOptions
{
  HttpMethod = "Post",    
  OnSuccess = "Loaded"    
}))
{
   <input type="text" name="q" />
   <button type="submit" value="Search">Search</button>
}

<script type="text/javascript">
   function Loaded(result) {
      alert('result ' + result.Name);
   }
</script>

//控制器

[HttpPost]
public ActionResult Search(string q)
{

}