如何从View到Controller

时间:2016-12-27 18:53:32

标签: c# asp.net model-view-controller

喜欢标题

我一步一步地举例

我可以将SelectItemList从控制器传递给View来执行DropDownList

但我无法从View到Controller获取值

有人可以教我如何使用MVC

这是我的 控制器

        var Course = getCourseList.getCourse();
        if (Course.Any())
        {
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var course in Course)
            {
                items.Add(new SelectListItem()
                {
                    Text = course.RC_NAME,
                    Value = course.RC_MAJORCODE,
                });
            }
            ViewBag.SelectList = items;
        }

这是我的观点

    @using YTDT_OAuth.Models
@model List<CourseInfo>
@using (Html.BeginForm("CourseSave", "Home"))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <table class="table">
                <tr>
                    <th scope="row">first</th>
                    <td>@Html.DropDownList("SelectList", null, "default", new {@id = "a" })</td>
                    @foreach (var item in Model)
                    {
                        <p>@item.RC_MAJORCODE</p>
                    }
                </tr>
        </table>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

我想在这个控制器中做点什么

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult CourseSave(CourseInfo CourseDataz)
{
    // the value is received in the controller.

    return View();
}

1 个答案:

答案 0 :(得分:1)

我个人喜欢使用强类型 模型 而不是ViewBag。 ViewData和ViewBag是动态的,它们会抛出运行时错误而不是编译时错误。

示例代码

模型

public class CourseInfo
{
    public string SelectedMajorCode { get; set; }
    public IList<SelectListItem> AvailableMajorNames { get; set; }

    public CourseInfo()
    {
        AvailableMajorNames = new List<SelectListItem>();
    }
}

视图

@model DemoMvc.Models.CourseInfo
@using (Html.BeginForm("Index", "Home"))
{
    @Html.DropDownListFor(m => m.SelectedMajorCode, Model.AvailableMajorNames)
    <input type="submit" value="Submit"/>

}

控制器

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new CourseInfo
        {
            AvailableMajorNames = GetColorListItems()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(CourseInfo model)
    {
        if (ModelState.IsValid)
        {
            var majorCode = model.SelectedMajorCode;
            return View("Success");
        }
        // If we got this far, something failed, redisplay form
        // ** IMPORTANT : Fill AvailableMajorNames again; 
        //    otherwise, DropDownList will be blank. **
        model.AvailableMajorNames = GetMajorListItems();
        return View(model);
    }

    private IList<SelectListItem> GetMajorListItems()
    {
        // This could be from database.
        return new List<SelectListItem>
        {
            new SelectListItem {Text = "One", Value = "1"},
            new SelectListItem {Text = "Two", Value = "2"}
        };
    }
}