Question

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?

Was it helpful?

Solution

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>

OTHER TIPS

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.

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>
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top