在应用程序中设置路由

时间:2014-07-30 09:33:40

标签: html asp.net-mvc

我正在创建一个MVC应用程序,我试图在单击“创建”按钮后转到某个页面。我只是在学习这个,我不确定如何用它来表达:P但我要解释一下。:

我有3个实体,YearWeekDay。它们被链接在一起,并有自己的创建和删除页面等。当你做一天时,你会说出它所在的年份和周。我想知道的是在我输入数据一天后点击“创建”之后,如何进入年度索引页面而不是第一天?当我点击“创建”时,它会显示所有创建日期的列表,我宁愿将其带到年份列表中。

Create页面的CSHTML代码在@using (Html.BeginForm())内,我尝试使用@using (Html.BeginForm("Index", "Year")),这确实将我带到了年度索引页面,但它没有创建所有

有人知道如何解决这个问题吗?

Day“创建”页面代码:

@model Utility3.Models.Day

@{
    ViewBag.Title = "Create";
}

<h1>Add Day</h1>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @*<h4>Day</h4>*@
        <hr />
        @Html.ValidationSummary(true)

    <div class="form-group">
        @Html.Label("Year", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("YearID", String.Empty)
            @Html.ValidationMessageFor(model => model.YearID)
        </div>
    </div>

    <div class="form-group">
        @Html.Label("Week Number", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("WeekID", String.Empty)
            @Html.ValidationMessageFor(model => model.WeekID)
        </div>
    </div>

    <div class="form-group">
        @Html.Label("Day of the Week", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("DayofWeek", new List<SelectListItem>
                {
                    new SelectListItem { Text = "Monday", Value = "Monday", Selected=true},
                    new SelectListItem { Text = "Tuesday", Value = "Tuesday"},
                    new SelectListItem { Text = "Wednesday", Value = "Wednesday"},
                    new SelectListItem { Text = "Thursday", Value = "Thursday"},
                    new SelectListItem { Text = "Friday", Value = "Friday"},
                    new SelectListItem { Text = "Saturday", Value = "Saturday"},
                    new SelectListItem { Text = "Sunday", Value = "Sunday"}
                }, "Select Day")
            @Html.ValidationMessageFor(model => model.DayofWeek)
        </div>
    </div>

    <div class="form-group">
        @Html.Label("Reading1", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reading1)
            @Html.ValidationMessageFor(model => model.Reading1)
        </div>
    </div>

    <div class="form-group">
        @Html.Label("Reading2", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reading2)
            @Html.ValidationMessageFor(model => model.Reading2)
        </div>
    </div>

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

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Year ActionResult Index

// GET: /Year/
    public ActionResult Index()
    {
        return View(db.Years.ToList());
    }

1 个答案:

答案 0 :(得分:1)

发布方法时,重定向到年份索引视图

[HttpPost]
public ActionResult Create(Utility3.Models.Day model)
{
  if (!ModelState.IsValid)
  {
    return View();
  }
  ....... // Save your model
  return RedirectToAction("Index", "Year");
}