2014-10-07 70 views
0

这实际上是两个问题之一。asp.net视图语法和html帮手

第一个问题是关于以下几点:

<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 => model.xxx手段,我知道它在做什么,但我不知道如何解释的语法。

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

foreach (var item in Model)

我怎能取代

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

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

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

谢谢!

+0

发现了一个回答我的问题上半年这里http://stackoverflow.com/questions/10467030/html-editorform -m-lambda-syntax-in-mvc – 2014-10-07 01:00:47

+0

请更激动地描述你的问题。你有一个清单,你想创建...什么?描述,然后对我和对答案会更好。 – W92 2014-10-07 01:26:41

+0

@ W92我有一个列表,我想为列表中的每个项目显示一个输入文本框,但是我希望每个文本框在其创建时都有文本,来自列表中每个项目的文本(来自项目的财产),谢谢你的帮助,我真的很感激 – 2014-10-07 01:29:45

回答

1

一个视图可以具有0或1模型,该模型的FROM控制器发送。, ViewNamemodel

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

然后在View可以使用model,如果:

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个参数它已经实现:

@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") 

例如:

,并在您controller控制器:

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

和有益这里回答的: ASP.NET MVC get textbox input value

编辑,回答究竟问题( w ^这在评论下面的问题hich containt:

我有一个列表,我想显示在列表中的每个项目的输入文本框,但我希望每个文本框内创建文本时,文本从列表中的每个项目

@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() 
(...) 
} 
+1

的链接请看这里:http://stackoverflow.com/questions/18873098/asp-net-mvc-razor-get-textbox-input-value – W92 2014-10-07 01:16:01

+0

谢谢你,我现在看:) – 2014-10-07 01:20:52

+0

the text是不是在文本框内创建时,我可以插入文本,但我想那里有文本已经从我的item.stringProperty – 2014-10-07 01:43:34

1

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

对于第二个,我认为

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

将正常工作

+0

谢谢,我会尽力回复你! – 2014-10-07 01:02:58

+0

我试过这个,但它不认为“stringProperty”作为某种原因的财产 – 2014-10-07 01:09:01

+0

是的,它取决于你的模型是什么类型,我看到你的模型,你正在从一个X型模型改变到一个型号列表但模型你在代码中使用var,所以我没有看到类型。我写的代码只适用于item是带有“stringProperty”属性的类的对象 – 2014-10-07 01:14:24