2014-10-07 18 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