0

This is actually two questions in one.

First question is about the following:

<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>

I don't understand what model => model.xxx means, I know what it's doing but i don't know how to interpret the syntax.

the second question is, if I have - for example -

foreach (var item in Model)

how can I replace

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

with

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

when I try this, it gives me errors, is there an overloaded EditorFor helper that accepts this?

thank you!

10
  • found an answer to the first half of my question here stackoverflow.com/questions/10467030/… Commented Oct 7, 2014 at 1:00
  • please describe your problem more excatly. You have a list and you want to create ...what?Describe, then will be better for me and for the answer. Commented Oct 7, 2014 at 1:26
  • @W92 I have a list, and I want to display an input text box for each item in the list, but I want each text box to have text inside when its created, text from each item in the list (coming from the item's property), Thank you for the help by the way, I really appreciate it Commented Oct 7, 2014 at 1:29
  • look at my answer (after edit) Commented Oct 7, 2014 at 1:38
  • thank you for the update, give me a minute to try it Commented Oct 7, 2014 at 1:39

2 Answers 2

1

One view can has 0 or 1 Model, which's sending from controller.

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);//
}

if your View's name "Index" then you can use second way for View, which contain 2 parameters: ViewName and model

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

then in your View you can use the model, if it has been implemented that:

@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.
}

if you want create Editor for item you must use:

  @Html.TextBox("YourName")

for example:

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

and in your controllerController:

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

and helpfully answer's here: ASP.NET MVC get textbox input value

Edit, answer about exactly problem (which containt in comment below question):

I have a list, and I want to display an input text box for each item in the list, but I want each text box to have text inside when its created, text from each item in the list (coming from the item's property)

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

and in your controller add MyMethod:

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

or

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

and if you want to have a better security page please read about Html.AntiForgeryToken: http://msdn.microsoft.com/en-us/library/dd470175(v=vs.118).aspx

@using(Html...())
{
@Html.AntiForgeryToken()
(...)
}
Sign up to request clarification or add additional context in comments.

2 Comments

the text is not inside the text box when it is created, I can insert text, but I want there to be text in it already coming from my item.stringProperty
1

I see you already got the answer of your first question.

For the second one, i think

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

will work fine

4 Comments

i tried this but it's not recognizing "stringProperty" as a property for some reason
yes it depends what type your model is, i tought seeing your model that you were changing from a model of type X to a model with type List<X> but you use var in your code so i don't see the type. the code i wrote works only if item is an object of a class with a "stringProperty" property
oh yea, sorry, my model is a list, so for each item in my list
@Abdul Ahmad please look at the answer below. read link from comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.