选定的项目列表和其他对象

时间:2012-07-11 14:00:01

标签: asp.net-mvc-3

我是asp.net mvc3编程的新手,我正在尝试构建一个特定的表单。我需要有一个带有用户字段的表单(我有),还有一个对象列表(在这种情况下是SStatus)。


我的表单:

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Création d'utilisateur</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Lastname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Lastname)
            @Html.ValidationMessageFor(model => model.Lastname)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Firstname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Firstname)
            @Html.ValidationMessageFor(model => model.Firstname)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Login)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Login)
            @Html.ValidationMessageFor(model => model.Login)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <p>Status</p>
        @{

//错误在这里形成                 

                @{
                    var list = ViewBag.listStatus as List<SStatus>;
                }
                @if (list != null)
                {
                    foreach(var status in list)
                    {
                        <option value=@status.ID>@status.Name</option> 
                    }
                }
            </select>
        }

        <p>
            <input type="submit" value="Création" />
        </p>
    </fieldset>
}

列表调用:


public ActionResult CreateUserView()
        {
            RestClient client = new RestClient(Resource.Resource.LocalUrlService);
            RestRequest request = new RestRequest("/status/all", Method.GET);
            var response = client.Execute(request);
            if(response.StatusCode == HttpStatusCode.OK)
            {
                List<SStatus> listSatus = JsonHelper.FromJson<List<SStatus>>(response.Content);
                ViewBag.listStatus = listSatus;
            }
            return View();
        }

表单帖子:


     [HttpPost]
            public ActionResult CreateUserView(Uuser userToCreate, string list)
            {
//list got the ID of SStatus. 
                if (ModelState.IsValid)
                {//Stuff}
    }

所以问题是:如何获取所选列表项?

问候。

1 个答案:

答案 0 :(得分:1)

使用视图模型模式。我仍然没有看到你的Uuser对象如何发送到视图(通过默认的[HttpGet]操作,但我想我看到了你要完成的任务。如果你以这种方式重构,你仍然可以使用内置验证,自动模型绑定等。

public class CreateUserViewModel
{
    public Uuser User { get; set; }
    public string Status { get; set; }
}

然后您的action参数应为CreateUserViewModel类型,例如

[HttpPost] 
public ActionResult CreateUserView(CreateUserViewModel vm)
{
    if(ModelState.IsValid)
    {
        {//Stuff}
}

我相信您需要name元素上的<select>属性才能发布。

<p>Status</p>
    @{
        <select name="Status">

虽然如果模型无效,你会遇到麻烦。您的观点应该针对CreateUserViewModel强烈输入,例如

@model YourModelNamespace.CreateUserViewModel

因此,您的Lastname属性可能如下所示(请注意.User

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Création d'utilisateur</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.User.Lastname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.User.Lastname)
            @Html.ValidationMessageFor(model => model.User.Lastname)
        </div>

最后,我猜您可以在ViewBag中保留可能的状态列表,但是您需要将所选值设置为@Model.Status。您可以考虑将CreateUserViewModel.Status更改为可以从控制器填充的List<SelectListItem>,例如您的GET操作应return View(CreateUserViewModel)

public ActionResult CreateUserViewModel()
{
    CreateUserViewModel vm = new CreateUserViewModel();
    vm.User = // set user
    vm.Status = new List<SelectListItem>()
    {
        new SelectListItem()
        {
            Value = "status1",
            Text = "status 1",
            Selected = false
        },
        new SelectListItem()
        {
            Value = "status2",
            Text = "status 2",
            Selected = true
        },
    };

    return View(vm); // this is the correct way to strongly type your view
}