public IList<Labour> Labours { get; set; }

public class Labour
{
    public string WorkDates { get; set; }
    public string WorkHour { get; set; }
}

I have this code above on my class.

How can i add this to my Create View Razor? can anyone tell me please. Also how i can add another Labour in running time.

Hi mich thank you for your reply but this is not i'm looking for this one is displaying list of ILIST.

What i want is Create new ILIST.

Member Avatar for LastMitch

What i want is Create new ILIST.

You only did 3 lines of code?

public class Labour
{
public string WorkDates { get; set; }
public string WorkHour { get; set; }
}

Here is a simple code to start :

public class LabourController : Controller{

[HttpGet]

public ActionResult Index(){

var labours = new List<Labour>();

labours.Add(new Labour {
     WorkDates = "12/12/2012 - 12/16/2012" 
     WorkHour = "40" 
    });

labours.Add(new Labour {
     WorkDates = "12/6/2012 - 12/11/2012" 
     WorkHour = "30" 
    });

    return View(labours);
}
}

and this:

@model IList<Labour> <h2>Labour Index</h2>

@foreach (var labours in Model){

<div>
<span class="labourWorkDates">@labour.WorkDates</span> 
<span class="labourWorkHour">@labour.WorkHour</span> 
</div>

}

and this:

@foreach (var labours in Model)
{
@ViewHelperMethods.Renderlabour(labour)
}

I hope you get the idea how it works.

Hi Mitch Thanks

I'm trying to integrate your code to mine

This is my ServiceDTO.cs

public class ServiceDTO
{
public IList<LabourDTO> Labours { get; set; }
}

This is my LabourDTO.cs

public class LabourDTO
{
    public string WorkDate { get; set; }
    public string WorkTime { get; set; }
}

In my ServiceController

public ActionResult Index()
{
    var service = new ServiceDTO();
    return View(service);
}

In my Index.cshtml

-i want this line to Create 2 textbox for Workdate & Worktime.

<p>@Html.EditorFor(model => model.Labours)</p>

<a href="#">Add another labour</a>

Please check my attachment. That is my planning to do like Phonenumbers. If i click Add another labour another two textbox for labour. Please help i'm begging.

Regards,
Ryan :)

Member Avatar for LastMitch

Please check my attachment. That is my planning to do like Phonenumbers. If i click Add another labour another two textbox for labour. Please help i'm begging.

What is the issue you are having? I'm not going to do your work. You have to explain to me what are you having problem with?

How can i add another TWO TEXTBOX.

As you can see in my attachment if you click the Add Phone Number it will be add another Phone Number. How to do that?

Member Avatar for LastMitch

You mean input box?

<textarea> tags are different <input> tags.

<input></input>
<input></input>

How long have you learn ASP.net?

@html.TextBoxFor(m => m.WorkDate)
@html.TextBoxFor(m => m.WorkHour)

This is one mitch.

How long have you learn ASP.net? newbie :( i've started last month. So i'm still learning on it.

Member Avatar for LastMitch

You can try this:

@using (Html.BeginForm("SaveData","ControllerName", FormMethod.Post))
{        
@html.TextBoxFor(m => m.WorkDate)
@html.TextBoxFor(m => m.WorkHour)

<input type="submit" value="Save" />
}

-

[HttpPost]
public ActionResult SaveData(FormCollection form)
{
return View();
}

Hi mitch how to add another

@html.TextBoxFor(m => m.WorkDate)
@html.TextBoxFor(m => m.WorkHour)?

Member Avatar for LastMitch

Hi mitch how to add another

What are you talking about?

Can you post what you have so far.

I got no idea why you want another set of TextBox?

I hope you understand you need a form which I gave you a code snippet with that already:

@using (Html.BeginForm("SaveData","ControllerName", FormMethod.Post))
{
@html.TextBoxFor(m => m.WorkDate)
@html.TextBoxFor(m => m.WorkHour)

<input type="submit" value="Save" />
}

-

[HttpPost]
public ActionResult SaveData(FormCollection form)
{
return View();
}

You another set?

Then add the 2 phone numbers here:

@model IList<Labour> <h2>Labour Index</h2>
@foreach (var labours in Model){
<div>
<span class="labourWorkDates">@labour.WorkDates</span>
<span class="labourWorkHour">@labour.WorkHour</span>
<span class="labourPhoneNumber1">@labour.PhoneNumber1</span>
<span class="labourPhoneNumber2">@labour.PhoneNumber2</span>
</div>
}

-

@using (Html.BeginForm("SaveData","ControllerName", FormMethod.Post)){
@html.TextBoxFor(m => m.WorkDate)
@html.TextBoxFor(m => m.WorkHour)
@html.TextBoxFor(m => m.PhoneNumber1)
@html.TextBoxFor(m => m.PhoneNumber2)
<input type="submit" value="Save" />
}

On the HTML side it looks like this:

<form method="post" action="">
<input id="WorkDate" name="WorkDate" type="textbox" />
<input id="WorkHour" name="WorkHour" type="textbox" />
<input id="PhoneNumber1" name="PhoneNumber1" type="textbox" />
<input id="PhoneNumber2" name="PhoneNumber2" type="textbox" />
<input type="submit" name="button" value="Send" />
</form>
Member Avatar for LastMitch

What issue you are having?

I think, you want when the user click "add" automatic and dinamically should appear two new inputs. If this is true, you need to create an action that recieves the click of "add" link and in this action add new model to your list and return the view.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.