c# - 使用 html 助手时没有发布值

标签 c# forms post asp.net-mvc-3 html-helper

我在 mvc3 中遇到问题。我不确定这是否是专门的 mvc3,但是我目前正在将其与 razor 引擎一起使用。不管怎样,我面临的问题是,我有一个表单,我使用 TextBoxFor、CheckBoxFor 等来渲染它。渲染工作完美无缺,除了当我尝试发布数据时,我基本上发布了一个带有空值的空表单。

这是我的模型:

public class SendReplyPmForm : PM
{
    public new string Text { get; set; }

    public bool IsOriginalDelete { get; set; }

    public int ReplyNr { get; set; }

    public string ReceiverName { get; set; }
}

我在 View 和模型之间有一个额外的 View 模型层,它包含有关该模型的额外参数

public class IndexViewModel
{
     public SendReplyPmForm SendReplyPmForm { get; set; }
      ...

这是我的观点

@using (Html.BeginForm("SendReply", "Pm", FormMethod.Post, new { id = "formSendMsg" }))
{
            @Html.TextBoxFor(model => model.SendReplyPmForm.ReceiverName, new { id = "ReceiverName" })
            <span id="spanChkText">Delete Original Message: @Html.CheckBoxFor(model => model.SendReplyPmForm.IsOriginalDelete, new { id = "chkIsOriginalDelete", value = 1 })</span>

    @{Html.RenderPartial("~/Areas/Forums/Views/Shared/Toolbar.cshtml");}

        <span class="spanLabel">Message</span>
            @Html.TextAreaFor(model => model.SendReplyPmForm.Text, new { id = "Text", rows = "10", cols = "65" })

    @{Html.RenderPartial("temp.cshtml");}

    @Html.HiddenFor(model => model.SendReplyPmForm.ReplyNr, new { id = "inputReplyNr", value = 0 })

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

}

这里我有 Controller

[HttpPost]
    public ActionResult SendReply(SendReplyPmForm SendReplyForm) {
       var ViewModel = new IndexViewModel();
       .
       .
       .

        return View("Index", ViewModel);
    }

奇怪的是,如果我使用纯 HTML 而不是 Html 助手,那么帖子就会顺利进行,没有任何问题。

在发布本文之前,我读过这篇文章( ASP.NET MVC’s Html Helpers Render the Wrong Value! ),但我不太确定我是否有同样的问题。 (例如:我的 Controller 中没有同名的操作),但事实上它也与纯 Html 一起使用,这让我思考。

你们知道如何将这个表单与 Html 助手一起使用吗?

最佳答案

我相信您的问题与 here 相同,所以需要添加前缀:

[HttpPost]
public ActionResult SendReply([Bind(Prefix="SendReplyPmForm")]SendReplyPmForm SendReplyForm)
{
  ...
}

关于c# - 使用 html 助手时没有发布值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4861143/

相关文章:

python - 在 python 中发送 header

python - 如何使用 django/python 从网络服务获取返回值

c# - 如何删除所有 ListBox 项目?

c# - 文本框自动滚动从 wp8.1 rt 中的代码后面添加文本

forms - Symfony2 表单 BooleanToStringTransformer 问题

javascript - 将文本添加到输入并触发 ajax 搜索

http - CakePHP 中的 http post 和 Redirect 有什么区别

c# - 强制Linq不延迟执行

c# - 在 C# 中交替排序或重新排序列表

php - 如何使用 box.net API 上传文件?