【问题标题】:ViewModel is not binding from the POST form when passed into a method as a parameter当作为参数传递给方法时,ViewModel 没有从 POST 表单绑定
【发布时间】:2011-04-20 05:49:39
【问题描述】:

我是 asp.net mvc 的新手,但我已经在各处彻底搜索过这个主题。

我有一个名为 PictureViewModel 的 ViewModel 类,它是 EditorTemplate 视图继承自的类:

<div class="editor-label"><%: Html.LabelFor(m=>m.Name) %></div> 
<div class="editor-field"><%: Html.TextBox("form.Name") %></div> 

<div class="editor-label"><%: Html.LabelFor(m=>m.Description) %></div> 
<div class="editor-field"><%: Html.TextArea("form.Description") %></div> 

<% Html.RenderAction("SelectCategory", "Category"); %>

<div class="editor-label"><label for="thumbFile">Thumb</label></div> 
<div class="editor-field"><input id="thumbFile" name="thumbFile" type="file"/></div> 

<div class="editor-label"><label for="fullFile">Full</label></div> 
<div class="editor-field"><input id="fullFile" name="fullFile" type="file"/></div> 

我还有文件字段和所有内容都发布到自己

<%@ Page Title="" Language="C#" MasterPageFile="/Views/Shared/Site_Table.Master"
 Inherits="System.Web.Mvc.ViewPage<MvcApplicationVelarPolya.Areas.Admin.Models.PictureViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Create</h2>

    <%
  using (Html.BeginForm("create", "Pictures", FormMethod.Post, new {enctype="multipart/form-data"})) 
   {%> 

    <%: Html.EditorForModel() %>

    <input type="submit" name="Create" />

<% }%>

</asp:Content>

这里是专门处理提交的代码:

    [HttpPost]
    public ActionResult Create([Bind(Prefix="form")] PictureViewModel pm)
    {
        try
        {
            var p = new PictureViewModel();
            UpdateModel(p, "",new[] {"Name", "Description"});
            ViewData["name"] = Request.Files[0].FileName;
            return View("New", p);
        }
        catch
        {
            return View();
        }
    }

无论是添加前缀,还是使用带有 UpdateModel 的 FormCollection 都无济于事。 我将不胜感激任何帮助! 安德鲁。

哦,是的,viewdata 已正确填充了文件名!

这也行不通 [HttpPost] public ActionResult Create(FormCollection fc/[Bind(Prefix="form")] PictureViewModel pm/) { 尝试 { var p = new PictureViewModel(); UpdateModel(p, "form",new[] {"Name", "Description"}); ViewData["name"] = Request.Files[0].FileName; // TODO: 在此处添加插入逻辑 返回视图(“新”,p); return RedirectToAction("索引"); } 抓住 { 返回视图(); } }

还有这段代码

        foreach (var k in fc)
        {
            Debug.WriteLine(k.ToString());
        }

正在生产

form.Name
form.Description
CategoryID
Create

【问题讨论】:

  • 添加您的 FormCollection 并查看您收到的密钥。在里面。
  • 我试过了,有4个键。

标签: asp.net-mvc-2 mode


【解决方案1】:

在您的编辑器模板中,您应该使用强类型助手(TextBoxForTextAreaFor),并且您可以摆脱魔术字符串和前缀:

<div class="editor-label"><%: Html.LabelFor(m => m.Name) %></div> 
<div class="editor-field"><%: Html.TextBoxFor(m => m.Name) %></div> 

<div class="editor-label"><%: Html.LabelFor(m => m.Description) %></div> 
<div class="editor-field"><%: Html.TextAreaFor(m => m.Description) %></div> 

<% Html.RenderAction("SelectCategory", "Category"); %>

<div class="editor-label"><label for="thumbFile">Thumb</label></div> 
<div class="editor-field"><input id="thumbFile" name="thumbFile" type="file"/></div> 

<div class="editor-label"><label for="fullFile">Full</label></div> 
<div class="editor-field"><input id="fullFile" name="fullFile" type="file"/></div> 

要处理上传的文件,您可以将它们添加为视图模型属性或操作参数:

public class PictureViewModel
{
    public string Name { get; set; }
    public string Description { get; set; }
    public HttpPostedFileBase ThumbFile { get; set; }
    public HttpPostedFileBase FullFile { get; set; }
}

...

[HttpPost]
public ActionResult Create(PictureViewModel pm)
{
    // Normally the pm variable should be fully 
    // initialized here from the POST request and you can use it directly
    // No need to call UpdateModel
    return View("New", pm);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 2014-08-18
    • 1970-01-01
    • 2019-07-30
    • 2016-07-07
    • 1970-01-01
    相关资源
    最近更新 更多