代码之家  ›  专栏  ›  技术社区  ›  Alexandr

MVC将值从视图传递到动态局部视图

  •  0
  • Alexandr  · 技术社区  · 7 年前

    我正在尝试创建一个局部视图,它将动态添加我输入的尽可能多的字段。我输入字段的数量,然后我想在形成的字段中选择一周中的几天,并在控制器中处理这些数据,但我在部分视图中传递天数方面存在问题。 :

    public class CoursesController : Controller
    {
        private SchoolContext db = new SchoolContext();
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "CourseId,Name,Language,LanguageProficiency,StartDate,EndDate,TeacherId,NumberOfLessonsPerWeek")] Course course)
        {
            if (ModelState.IsValid)
            {
                db.Courses.Add(course);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(course);
        }
        public ActionResult ShowDaysOfweek(int? countDays)
        {
            //countDays = 2;
            ViewBag.CountDays = countDays;
            var days =  new List<DayOfWeek> { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday };
            ViewBag.DaysOfWeek = new SelectList(days);
            return PartialView("ShowDaysOfweek");
        }
    }
    

    看法 我可以通过单击脚本中的按钮添加空间视图:

    @model SchoolManagementSystem.Models.Course
    @using (Ajax.BeginForm(new AjaxOptions { }))
    {
        @Html.AntiForgeryToken()
        <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" }
        <div class="form-group">
            @Html.Label("Number Of Lessons Per Week", htmlAttributes: new { @class = "control-label col-md-3" })
            <div class="col-md-5">
                @Html.TextBox("NumberOfLessonsPerWeek", null, htmlAttributes: new { @class = "form-control", @type = "number", @id = "numberOfLessonsPerWeek" })
            </div>
            <div>
                <input type="button" value="Add days" id="Show" class="btn btn-default" />
            </div>
        </div>
        <div id="ShowResults">
    
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
    }
    <script type="text/javascript">
        var url = '@Url.Action("ShowDaysOfweek", "Courses")';
        $('#Show').on("click", function () {
            var countDays = $('#numberOfLessonsPerWeek').val();
            url += '/?countDays=' + countDays;
             $('#ShowResults').load(url);
        })
    </script>
    

    :

    @for (var i = 0; i < ViewBag.CountDays; i++)
    {
    <div class="form-group">
        @Html.Label("Day of week", htmlAttributes: new { @class = "control-label col-md-3" })
        <div class="col-md-5">
            @Html.DropDownList("DaysOfWeek", (IEnumerable<SelectListItem>)ViewBag.DaysOfWeek, htmlAttributes: new { @class = "form-control", style = "height: 35px" })
        </div>
    </div>
    }
    

    有可能做点什么吗?谢谢

    1 回复  |  直到 7 年前
        1
  •  0
  •   Mannan Bahelim    7 年前

    首先使用部分视图url创建隐藏字段

    <input type="hidden" value="@Url.Action("ShowDaysOfweek", "Courses", new { countDays= "numberOfLessonsPerWeek" })" id="hdnURLShowDaysOfweek" />
    

    <script type="text/javascript">
        var url = $('#hdnURLShowDaysOfweek').val();
        $('#Show').on("click", function () {
        url = url.replace("numberOfLessonsPerWeek",$('#numberOfLessonsPerWeek').val());
                 $('#ShowResults').load(url);
        })
    </script>