0

I am generating a form list like this

foreach (projects p in projects)
                {
                    ViewBag.ProjectList += @"<div class='itemdiv memberdiv'>

                                                      @Using(Html.BeginForm()) {

and its being passed in to the html with the @html.raw(ViewBag.ProjectList) but the issue is that the @using is being passed as a string and i cannot do it outside the controller Viewbag string because it does not recognize the @using from the controller.

how do i generate a submission form list like this from within the controller?

tereško
  • 58,060
  • 25
  • 98
  • 150

3 Answers3

1

Since you are not adding any additional attributes to your form, you can probably get by with writing out the html form tag instead of wrapping it in back-end code.

foreach (projects p in projects)
{
    ViewBag.ProjectList += @"<div class='itemdiv memberdiv'>
                                 <form method='POST'>...</form>
ohiodoug
  • 1,493
  • 1
  • 9
  • 12
0

You have to build a form tag directly. You can't use the MVC Html helpers since that is c# code that has to execute.

TGH
  • 38,769
  • 12
  • 102
  • 135
0

I would not use viewbag, I would use a model. Then loop through each of items in your model. Since your putting multiple forms on a page, you need to specify an action for each, else your submit buttons will all goto the same action.

Model

public class Projects
{
  public string Action {get; set;}
  public string Controller {get; set;}
  public string FormStuff {get; set;}
}  

View

 @model Projects

    @foreach (projects p in Model)
    {
      <div class='itemdiv memberdiv'>
        @Using(Html.BeginForm(p.Action, p.Controller))
        {
          @p.FormStuff
        }
      </div>
    }
TK-421
  • 387
  • 2
  • 11