【问题标题】:asp.net view syntax and html helperasp.net 视图语法和 html 助手
【发布时间】:2014-12-01 08:15:51
【问题描述】:

这实际上是两个问题合二为一。

第一个问题是关于以下内容:

<div class="form-group">
 @Html.LabelFor(model => model.xxx, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
  @Html.EditorFor(model => model.xxx , new { htmlAttributes = new { @class = "form-control" } })
  @Html.ValidationMessageFor(model => model.xxx, "", new { @class = "text-danger" })
 </div>
</div>

我不明白model =&gt; model.xxx 是什么意思,我知道它在做什么,但我不知道如何解释语法。

第二个问题是,如果我有 - 例如 -

foreach (var item in Model)

如何替换

@Html.EditorFor(model =&gt; model.xxx , new { htmlAttributes = new { @class = "form-control" } })

@Html.EditorFor(item.stringProperty , new { htmlAttributes = new { @class = "form-control" } })

当我尝试这个时,它给了我错误,是否有一个重载的 EditorFor 帮助器接受这个?

谢谢!

【问题讨论】:

  • 在这里找到了我问题前半部分的答案stackoverflow.com/questions/10467030/…
  • 请更准确地描述您的问题。你有一个列表,你想创建......什么?描述,那么对我和答案都会更好。
  • @W92 我有一个列表,我想为列表中的每个项目显示一个输入文本框,但我希望每个文本框在创建时都有文本,其中每个项目的文本清单(来自物品的属性),顺便感谢您的帮助,非常感谢
  • 看看我的答案(编辑后)
  • 谢谢你的更新,给我一分钟试试

标签: c# asp.net asp.net-mvc html-helper


【解决方案1】:

我看到你已经得到了第一个问题的答案。

我认为是第二个

@Html.EditorFor(item => item.stringProperty , new { htmlAttributes = new { @class = "form-control" } })

会正常工作

【讨论】:

  • 我试过了,但由于某种原因它没有将“stringProperty”识别为属性
  • 是的,这取决于您的模型是什么类型,我很难看到您的模型是从 X 类型的模型更改为 List 类型的模型,但是您在代码中使用了 var 所以我看不到类型。我编写的代码仅在 item 是具有“stringProperty”属性的类的对象时才有效
  • 哦,是的,对不起,我的模型是一个列表,所以对于我列表中的每个项目
  • @Abdul Ahmad 请看下面的答案。从评论中阅读链接
【解决方案2】:

一个视图可以有 0 或 1 个模型,这些模型是从控制器发送的。

public class Person
{
    public string Name {get;set;}
    public int Age {get;set;}
}

public ViewResult Index()
{
    Person p = new Person() { Name = "P1", Age = 100};
    return View(p);//
}

如果您的视图名称为“索引”,那么您可以对视图使用第二种方式,其中包含 2 个参数: ViewNamemodel

return View("Index", model: p);

然后在您的View 中,您可以使用model,如果它已实现:

@model Person//and remember about namespace
@
{
 ...
}

@using(Html.BeginForm("ActionName", "controllerName", FormMethod.Post))
{
     @Html.EditorFor(model => model.Name); // it create input, check in F12 in your browse - then you can exactly understand.
}

如果你想为你必须使用的项目创建编辑器:

  @Html.TextBox("YourName")

例如:

@using(Html.BeginForm("Action", "controller")
{
   @Html.TextBox("YourName")
   <input type="submit" value="ok"/>
}

在您的controllerController 中:

public ViewResult Action(string YourName)
{
     //here you got value for string YourName
     return View();
}

并有帮助的答案在这里: ASP.NET MVC get textbox input value

编辑,准确回答问题(包含在问题下方的评论中):

我有一个列表,我想为列表中的每个项目显示一个输入文本框,但我希望每个文本框在创建时都有文本,列表中每个项目的文本(即将到来来自项目的属性)

@foreach(var item in Model)
@using(Html.BeginForm("MyMethod", "Controller"))
{
   @Html.TextBox("item", item)
   <input type="submit" value="ok" />
}

并在您的控制器中添加 MyMethod:

[HttpPost]
public ViewResult MyMethod(string item)
{
 ...
}

[HttpPost]
public ViewResult MyMethod(int item) //if it's an int
{
 ...
}

如果您想拥有更好的安全页面,请阅读Html.AntiForgeryTokenhttp://msdn.microsoft.com/en-us/library/dd470175(v=vs.118).aspx

@using(Html...())
{
@Html.AntiForgeryToken()
(...)
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
  • 1970-01-01
  • 2011-05-05
  • 2011-10-30
相关资源
最近更新 更多