razor视图下viewdata传递html被转义,在mvc 4 Razor视图中使用它时,Html.DisplayFor不会返回提交值(Html.DisplayFor doesn't return ...

在mvc 4 Razor视图中使用它时,Html.DisplayFor不会返回提交值(Html.DisplayFor doesn't return value on submit using it in mvc 4 Razor view)

我将一些虚拟数据传递给我的View只是为了有一些工作:

public ActionResult Index()

{

ViewBag.Message = "Create table";

var model = new List();

model.Add(new Auction

{

Title = "First Title",

Description = "First Description"

});

model.Add(new Auction

{

Title = "Second Title",

Description = "Second Description"

});

model.Add(new Auction

{

Title = "Third Title",

Description = "Third Description"

});

model.Add(new Auction

{

Title = "Fourht Title",

Description = "Fourth Description"

});

return View(model);

}

我以我的观点显示了这一点:

@model List

@{

ViewBag.Title = "Home Page";

}

@using (Html.BeginForm())

{

@for (var i = 0; i < Model.Count(); i++)

{

@Html.HiddenFor(x => x[i].Id)

@Html.DisplayFor(x => x[i].Title)

@Html.EditorFor(x => x[i].Description)

}

Save

}

现在我想对提交的数据进行一些操作:

[HttpPost]

public ActionResult Index(List model)

{

var test = model;

model[1].Title = "Test";

return View(model);

}

但是当我调试这个时,我发现Description属性被发回,但Title属性没有价值。 正如你所看到的,我用@Html.DisplayFor(x => x[i].Title)显示它们。 这是一些默认行为还是我做错了什么?

I pass some dummy data to my View just to have something to work with :

public ActionResult Index()

{

ViewBag.Message = "Create table";

var model = new List();

model.Add(new Auction

{

Title = "First Title",

Description = "First Description"

});

model.Add(new Auction

{

Title = "Second Title",

Description = "Second Description"

});

model.Add(new Auction

{

Title = "Third Title",

Description = "Third Description"

});

model.Add(new Auction

{

Title = "Fourht Title",

Description = "Fourth Description"

});

return View(model);

}

I display this in my view :

@model List

@{

ViewBag.Title = "Home Page";

}

@using (Html.BeginForm())

{

@for (var i = 0; i < Model.Count(); i++)

{

@Html.HiddenFor(x => x[i].Id)

@Html.DisplayFor(x => x[i].Title)

@Html.EditorFor(x => x[i].Description)

}

Save

}

and now I want to make some manipulation of the submitted data :

[HttpPost]

public ActionResult Index(List model)

{

var test = model;

model[1].Title = "Test";

return View(model);

}

But when I debug this I see that the Description properties are send back but the Title properties don't have value. As you can see above I display them with @Html.DisplayFor(x => x[i].Title). Is this some default behaviour or am I doing something wrong?

原文:https://stackoverflow.com/questions/16388263

更新时间:2019-12-31 06:56

最满意答案

@Html.DisplayFor不输出输入字段(或任何表单字段),因此它不是表单帖子值的一部分。

如果您希望将标题作为表单的一部分发布,则可能需要添加附加内容

@Html.HiddenFor(x => x[i].Title)

@Html.DisplayFor doesn't output an input field (or any form field for that matter) so it isn't part of the values the form posts.

If you want the title posted as part of the form you may want to add an additional

@Html.HiddenFor(x => x[i].Title)

相关问答

你需要添加一个 @Html.HiddenFor(m => m.EmailAddress)

DisplayFor不会在POST中发送任何内容,它不会创建输入... 顺便说一下,一个 @Html.HiddenFor(m => m.Id) // or anything which is the model key 将是有用的 you'll need to add a @Html.HiddenFor(m => m.EmailAddress)

DisplayFor won't send anything

...

DisplayFor帮助程序为给定类型呈现相应的显示模板。 例如,如果您想以某种方式个性化此模板,则应该使用它。 或与集合属性。 与集合属性一起使用时,相应的模板将自动为集合的每个元素呈现。 以下是它的工作原理: @Html.DisplayFor(x => x.SomeProperty)

将呈现给定类型的默认模板 。 例如,如果您使用某些格式化选项修饰了视图模型属性,它将遵循以下选项: [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]

pu

...

DisplayFor不能像其他工作*For帮手。 就像EditorFor一样,这就是所谓的“模板助手”。 换句话说,它呈现的是由可以修改的模板控制的。 重要的是,对于这两种方法,如果你在MSDN中查找他们的文档,你会发现这个参数与其他helper的htmlAttributes正常相对应,而是指向具有这两个参数的additionalViewData 。 这是因为它们的输出再次受到ViewData基本视图控制。 此外,特别是在DisplayFor中,默认模板几乎只输出值,而没有HTML。 例如,如果传

...

部分视图名称必须是Person.ascx放置在shared\DisplayTemplates文件夹中 The partial view name must be Person.ascx placed in shared\DisplayTemplates folder

您应该只需要在文档准备好时在表单上调用.ajaxForm 。 .ajaxForm本身只是准备 ajax提交的表单,所以当按下提交按钮时,已经太晚了。 我将从submit按钮中删除提交处理程序(我还在这里使用submit类型的input ):

并添加以下内容: $(function () {

$("#add_address").ajaxForm({

url: '~/Home/Add',

...

@Html.DisplayFor不输出输入字段(或任何表单字段),因此它不是表单帖子值的一部分。 如果您希望将标题作为表单的一部分发布,则可能需要添加附加内容 @Html.HiddenFor(x => x[i].Title) @Html.DisplayFor doesn't output an input field (or any form field for that matter) so it isn't part of the values the form posts. If you w

...

什么是objGenController? 你为什么要调用它而不是直接调用CheckTenantControl? 或者,试试这个: public ActionResult Index()

{

ViewBag.TenantControl = objGenController.CheckTenantControl();

var result = GetTenantData();

return View(result);

}

public List Chec

...

DisplayFor帮助器可以利用DisplayTemplates来获取模型(及其属性)。 鉴于您正在将HTML填充到这些属性中,您只需要直接输出它们。 但是,因为MVC对所有输出进行编码,所以您还需要引入@Html.Raw()帮助器。 所以,如下所示: @Html.Raw(Model.Url1)

但是,就模型使用而言,这对MVC来说并不是一个好习惯。 我建议(如果必须使用HTML)使用IHtmlString接口作为属性类型(现在它直接使用@Model.Url1输出)或制作自定义对象和显示模板(

...

在您的操作上添加此属性 [ValidateInput(false)]

这应该可以解决您的问题 So after further investigation into how ASP MVC maps form fields to the model class and examining the HTML emitted to the browser, I found that this was an issue with the name of the property in my WebCo

...

Html.PagedListPager可以使用一个附加参数,该参数是一个表达式,告诉它如何生成下一页的URL: @Html.PagedListPager((IPagedList)ViewBag.OnePageOfItems, page => SomeMethodToGetUrlFor(page))

您需要做的基本上是保留页面的当前URL(具有筛选器等所有适当的查询字符串参数)并添加或更新page参数。 但是,“添加或更新”部分会使这一点变得棘手:例如,您不能只将&page=N到URL的末尾,因为

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值