1

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

所以问题是:如何获得选中的列表项?

问候。

4

1 回答 1

1

使用视图模型模式。我仍然看不到您的Uuser对象是如何发送到视图的(通过默认[HttpGet]操作,但我想我看到了您要完成的工作。)如果您以这种方式重构,您仍然可以使用内置的-在验证、自动模型绑定等方面。

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

那么你的动作参数应该是类型,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.StatusList<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
}
于 2012-07-11T14:42:53.283 回答