문제

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?

도움이 되었습니까?

해결책

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>

다른 팁

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>
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top