MVC传递值从视图到动态部分视图



我正在尝试做出部分视图,这将动态添加与我输入一样多的字段。I输入字段数,然后我想在形成的字段中选择一周的几天,并在控制器中使用此数据进行一些工作,但是在部分视图中,我的传递数天有问题。控制器

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 我可以通过单击脚本中的按钮添加patial视图:

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

相关内容

  • 没有找到相关文章

最新更新