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

MVC Pass value from view to dynamic partial view

我正在尝试创建一个局部视图,它将动态添加与我输入的字段一样多的字段。我输入字段数,然后我想在形成的字段中选择星期几,并在控制器中使用这些数据做一些工作,但我在部分视图中传递天数时遇到问题。 控制器

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

在此 View 中,我可以通过单击脚本中的按钮添加局部视图:

@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>
}

有没有可能做点什么?谢谢!

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

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

在javascript中读取url并替换参数

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