【问题标题】:Asp.net MVC 3 "parameter conversion from type 'System.String' to type failed" when using SelectList Dropdown box使用 SelectList 下拉框时,Asp.net MVC 3“从类型'System.String'到类型的参数转换失败”
【发布时间】:2012-02-24 14:07:35
【问题描述】:

我被困住了,在查了几个小时之后,我想我需要更多的眼球。

情况如下:

这是一个带有 Entity Framework 4 项目的 Asp.Net MVC3。我有两节课。一个配置文件和另一个操作。两者之间存在一对多的关系。这是代码的简化视图:

public class ConfigurationFile
{
    [Key, Required]
    [Column(TypeName = "uniqueidentifier")]
    public Guid Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Column(TypeName = "uniqueidentifier")]
    [Required]
    public Guid ActionId { get; set; }

    public virtual Models.Action Action { get; set; }
}

public class Action
{
    [Key, Required]
    [Column(TypeName = "uniqueidentifier")]
    public Guid Id { get; set; }

    [Required]
    public string ActionValue { get; set; }
}

然后我想创建一个新的ConfigurationFile,并且是我的两个控制器方法(此时,这是95%的Visual Studio 10生成的代码):

// db is my context class.
//
// GET: /Configuration/Create
public ActionResult Create()
{
    ViewBag.ActionId = new SelectList(db.Actions, "Id", "ActionValue");
    return View();
}

//
// POST: /Configuration/Create
[HttpPost]
public ActionResult Create(Models.ConfigurationFile configurationfile)
{
    if (ModelState.IsValid)
    {
        configurationfile.Id = Guid.NewGuid();
        db.ConfigurationFiles.Add(configurationfile);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    ViewBag.ActionId = new SelectList(db.Actions, "Id", "ActionValue", configurationfile.ActionId);
    return View(configurationfile);
}

这是我的 Create 视图的 sn-p:

@model MyProject.Areas.ConfigurationFile.Models.ConfigurationFile

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Configuration File</legend>

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

        <div class="editor-label">
            @Html.LabelFor(model => model.ActionId, "Action")
        </div>
        <div class="editor-field">
            @Html.DropDownList("ActionId", String.Empty)
            @Html.ValidationMessageFor(model => model.ActionId)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

当我打开 Create 页面时,我可以清楚地看到 Action 类的下拉列表很好(正确的值 -- Action.Id -- 和文本 -- Action.ActionValue -- )但是当我提交表单时,我有以下错误:“从类型'System.String'到类型'MyProject.Models.Action'的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换。”

请帮忙!!

【问题讨论】:

标签: asp.net entity-framework drop-down-menu type-conversion


【解决方案1】:

目前,MVC 无法将您的下拉列表从您的视图连接到您的 ConfigurationFile 对象的 ActionId。

我会尝试替换这一行:

@Html.DropDownList("ActionId", String.Empty)

为此

@Html.DropDownListFor(model => model.ActionId, ViewBag.ActionId)

除此之外,我想不出你还做错了什么。 希望对您有所帮助!

【讨论】:

  • 我对其他模型也有同样的想法,而 MVC 可以为这些模型建立关联。此外,它不起作用,甚至可以编译:“'System.Web.Mvc.HtmlHelper<...models.configurationfile>' 没有名为 'DropDownListFor' 的适用方法,但似乎具有该名称的扩展方法。扩展方法无法动态调度。考虑强制转换动态参数或调用扩展方法而不使用扩展方法语法。<...>\Areas\ConfigurationFile\Views\Configuration\Create.cshtml" 我以另一种方式解决了它(见我的自我回复如下)。还是谢谢
【解决方案2】:

这就是我绕过问题的方法。我只是这样改变了我的控制器:

Models.Action act = db.Actions.Find(configurationfile.ActionId);
ModelState.Clear();
configurationfile.Action = act;

TryValidateModel(configurationfile);

然后,验证就OK了。有点 hacky(以及可能对 DB 造成的另一个打击),但至少,我可以继续前进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多