不能让选择下拉值绑定到查看模型属性 [英] Can't get selected drop down value to bind to view model property

查看:98
本文介绍了不能让选择下拉值绑定到查看模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法绑定一个下拉列表,以正确的财产在我看来,模型的选择的价值。我看不到我在做什么错在这里。我已经把code,应有助于表明我在下面做什么。我忽略了一些东西,如所有文件夹视图模型的财产的人口,它只是一个名为 ImageGalleryFolder

对象的简单列表

每一次的形式帖子后面, ParentFolderId 属性是没有失败空。这是推动我疯了,我已经浪费了很多时间试图去解决它。

任何人都可以看到的东西,我做错了?

这是视图模型

 公共类ImageGalleryFolderViewModel
{
    [需要]
    公共字符串名称{搞定;组; }    公众诠释标识{搞定;组; }
    公共字符串CoverImageFileName {搞定;组; }
    公共HttpPostedFileBase UploadedFile的{搞定;组; }
    公共字符串ParentFolderId {搞定;组; }
    公众的IList< ImageGalleryFolder> {所有文件夹搞定;组; }
}

下面是视图code

  @using Payntbrush。presentation.Demo.MVC3.Areas.Admin
@model Payntbrush。presentation.Demo.MVC3.Areas.Admin.Models.ImageGalleryFolderViewModel@ {
    ViewBag.Title =创建一个新的库文件夹;
}< H2> @ ViewBag.Title< / H><脚本的src =@ Url.Content(〜/脚本/ jquery.validate.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.min.js)TYPE =文/ JavaScript的>< / SCRIPT>@using(Html.BeginForm((字符串)ViewBag.Action +文件夹,投资组合,FormMethod.Post,新{ID =CreateFolder,ENCTYPE =的multipart / form-data的}))
{
    @ Html.ValidationSummary(真)        如果(((字符串)ViewBag.Action).ToLower()== FormConstants.Edit.ToLower())
        {
            @ Html.HiddenFor(M = GT; m.Id)
            @ Html.HiddenFor(M = GT; m.CoverImageFileName)
            @ Html.HiddenFor(M = GT; m.ParentFolderId)
        }        < D​​IV CLASS =编辑标记>
            @ Html.LabelFor(型号=> model.Title)
        < / DIV>
        < D​​IV CLASS =主编场>
            @ Html.EditorFor(型号=> model.Title)
            @ Html.ValidationMessageFor(型号=> model.Title)
        < / DIV>        < D​​IV CLASS =编辑标记>
            @ Html.LabelFor(型号=> model.UploadedFile)
        < / DIV>
        < D​​IV CLASS =主编场>
            <输入类型=文件名称=UploadedFile的/>
            @ Html.ValidationMessageFor(型号=> model.UploadedFile)
        < / DIV>
    {
        //计数>图1是这里重要。如果只有1个文件夹,那么我们仍然没有显示在下拉
        //作为一个子文件夹不能有自己,因为它是自己的父母。
    }
    如果(@ Model.AllFolders.Count→1)
         {
             < D​​IV CLASS =编辑标记>
                 选择一个父文件夹(可选)
             < / DIV>
             < D​​IV CLASS =主编场>
                    @ Html.DropDownListFor(M = GT; m.ParentFolderId,新的SelectList(Model.AllFolders,ID,标题))
             < / DIV>
         }
    &所述p为H.;
            <输入类型=提交值=保存/>
        &所述; / P>
}< D​​IV>
    @ Html.ActionLink(返回目录,索引)
< / DIV>

我ommitted我的观点,但是这是我的形式看起来时,在浏览器中呈现等。该表格​​看起来从我所看到的好?

 <表格ID =CreateFolder行动=/ SlapDaBass /组合/ EditFolder / 1ENCTYPE =的multipart / form-data的方法=后>
<输入数据-VAL =真正的数据-VAL-数=ID字段必须是一个数字。数据-VAL-所需=是必需的ID字段。 ID =IDNAME =ID类型=隐藏值=1/>
<输入ID =CoverImageFileNameNAME =CoverImageFileName类型=隐藏值=/>
<输入ID =ParentFolderIdNAME =ParentFolderId类型=隐藏值=/>
< D​​IV CLASS =编辑标记>             <标签=标题>标题< /标签>            < / DIV>            < D​​IV CLASS =主编场>                <输入类=文本框单行数据-VAL =真正的数据-VAL-所需=是必需的Title字段 ID =标题名称=标题类型=文本VALUE =测试/>                <跨度类=现场验证,有效的数据valmsg换=标题数据valmsg替换=真正的>< / SPAN>            < / DIV>            < D​​IV CLASS =编辑标记>                <标签=UploadedFile的>&UploadedFile的LT; /标签>            < / DIV>            < D​​IV CLASS =主编场>                <输入类型=文件名称=UploadedFile的/>                <跨度类=现场验证,有效的数据valmsg换=UploadedFile的数据valmsg替换=真正的>< / SPAN>            < / DIV>                 < D​​IV CLASS =编辑标记>                     选择一个父文件夹(可选)                 < / DIV>                 < D​​IV CLASS =主编场>                        <选择一个id =ParentFolderIdNAME =ParentFolderId>
                           <期权价值=1>测试与LT; /选项>
                           <期权价值=2>测试2版; /选项>                         < /选择>                 < / DIV>        &所述p为H.;                <输入类型=提交值=保存/>            &所述; / P>    < /表及GT;

这是控制器动作:

  [HttpPost]
        公众的ActionResult EditFolder(INT ID,ImageGalleryFolderViewModel模型)
        {
            如果(ModelState.IsValid)
            {
                Services.PortfolioService.UpdateFolder(model.MapToDomainModel(),model.UploadedFile);
                回家;
            }
            返回查看();
        }


解决方案

改变的数据类型 ParentFolderId

 公共类ImageGalleryFolderViewModel
{
    [需要]
    公共字符串名称{搞定;组; }    公众诠释标识{搞定;组; }
    公共字符串CoverImageFileName {搞定;组; }
    公共HttpPostedFileBase UploadedFile的{搞定;组; }
    公众诠释ParentFolderId {搞定;组; }
    公众的IList< ImageGalleryFolder> {所有文件夹搞定;组; }
}

也使用HTML帮手DropDownList的

 <%:
     Html.DropDownListFor(
           模型=> model.ParentFolderId,
           新的SelectList(
                  新的List<对象> {
                       新{值= 1,文本=测试},
                       新{值= 2,文本=Test2的},
                       新{值= 3,文本=Test3的}
                    },
                  值,
                  文本
           )
        )
%GT;

我希望你是强类型化视图像

 公众的ActionResult EditFolder()
    {        返回查看(新ImageGalleryFolderViewModel());
    }

I'm having trouble binding the selected value of a drop down list to the correct property in my view model. I can't see what I am doing wrong here. I've put the code that should help show what I'm doing below. I've omitted some things such as the population of the 'AllFolders' property of the view model, as it's just a simple List with an object called ImageGalleryFolder.

Every time the form posts back, the ParentFolderId property is null without fail. This is driving me crazy and I've wasted a lot of time trying to work it out.

Can anyone see something I'm doing wrong?

This is the view model

public class ImageGalleryFolderViewModel
{
    [Required]
    public string Title { get; set; }

    public int Id { get; set; }
    public string CoverImageFileName { get; set; }
    public HttpPostedFileBase UploadedFile { get; set; }
    public string ParentFolderId { get; set; }
    public IList<ImageGalleryFolder> AllFolders { get; set; } 
}

Here is the view code

@using Payntbrush.Presentation.Demo.MVC3.Areas.Admin
@model Payntbrush.Presentation.Demo.MVC3.Areas.Admin.Models.ImageGalleryFolderViewModel

@{
    ViewBag.Title = "Create A New Gallery Folder";
}

<h2>@ViewBag.Title</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm((string)ViewBag.Action + "Folder", "Portfolio", FormMethod.Post, new { Id = "CreateFolder", enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)

        if(((string)ViewBag.Action).ToLower() == FormConstants.Edit.ToLower())
        {
            @Html.HiddenFor(m => m.Id)
            @Html.HiddenFor(m => m.CoverImageFileName)
            @Html.HiddenFor(m => m.ParentFolderId)
        }

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

        <div class="editor-label">
            @Html.LabelFor(model => model.UploadedFile)
        </div>
        <div class="editor-field">
            <input type="file" name="UploadedFile"/>
            @Html.ValidationMessageFor(model => model.UploadedFile)
        </div>


    {
        // Count > 1 is important here. If there is only 1 folder, then we still don't show the drop down
        // as a child folder can't have itself as it's own parent.
    }
    if(@Model.AllFolders.Count > 1)
         {
             <div class="editor-label">
                 Choose a parent folder (optional)
             </div>
             <div class="editor-field">
                    @Html.DropDownListFor(m => m.ParentFolderId, new SelectList(Model.AllFolders, "Id", "Title"))
             </div>


         }


    <p>
            <input type="submit" value="Save" />
        </p>
}

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

I've ommitted my view, but this is what my form looks like when rendered in the browser. The form looks good from what I can see?

   <form Id="CreateFolder" action="/SlapDaBass/Portfolio/EditFolder/1" enctype="multipart/form-data" method="post">
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="1" />
<input id="CoverImageFileName" name="CoverImageFileName" type="hidden" value="" />
<input id="ParentFolderId" name="ParentFolderId" type="hidden" value="" />        


<div class="editor-label">

             <label for="Title">Title</label>

            </div>

            <div class="editor-field">

                <input class="text-box single-line" data-val="true" data-val-required="The Title field is required." id="Title" name="Title" type="text" value="Test" />

                <span class="field-validation-valid" data-valmsg-for="Title" data-valmsg-replace="true"></span>

            </div>

            <div class="editor-label">

                <label for="UploadedFile">UploadedFile</label>

            </div>

            <div class="editor-field">

                <input type="file" name="UploadedFile"/>

                <span class="field-validation-valid" data-valmsg-for="UploadedFile" data-valmsg-replace="true"></span>

            </div>

                 <div class="editor-label">

                     Choose a parent folder (optional)

                 </div>

                 <div class="editor-field">

                        <select id="ParentFolderId" name="ParentFolderId">
                           <option value="1">Test</option>
                           <option value="2">Test 2</option>

                         </select>

                 </div>

        <p>

                <input type="submit" value="Save" />

            </p>

    </form>

And this is the controller action:

[HttpPost]
        public ActionResult EditFolder(int id, ImageGalleryFolderViewModel model)
        {
            if (ModelState.IsValid)
            {
                Services.PortfolioService.UpdateFolder(model.MapToDomainModel(), model.UploadedFile);
                return Home;
            }
            return View();
        }

解决方案

change the data type of the ParentFolderId

public class ImageGalleryFolderViewModel
{
    [Required]
    public string Title { get; set; }

    public int Id { get; set; }
    public string CoverImageFileName { get; set; }
    public HttpPostedFileBase UploadedFile { get; set; }
    public int ParentFolderId { get; set; }
    public IList<ImageGalleryFolder> AllFolders { get; set; } 
}

also use the Html helper for the dropdownlist

<%: 
     Html.DropDownListFor(
           model => model.ParentFolderId , 
           new SelectList(
                  new List<Object>{ 
                       new { value = 1 , text = "Test"  },
                       new { value = 2 , text = "Test2" },
                       new { value = 3 , text = "Test3"}
                    },
                  "value",
                  "text"
           )
        )
%>

i hope you are strongly typing your view like

    public ActionResult EditFolder()
    {

        return View(new ImageGalleryFolderViewModel());
    }

这篇关于不能让选择下拉值绑定到查看模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆