简体   繁体   English

将 IEnumerable 传递给视图以生成表单,然后将表单数据传回 controller

[英]Passing an IEnumerable to a view to generate a form and then passing form data back to controller

I'm working on a project for my office.我正在为我的办公室做一个项目。 The end result I'm looking for is that a boilerplate letter is pulled from a database, the sections that need specific input are extracted, a form is generated from those sections, the form then returns user data, and the letter is rebuilt with the user data integrated into the text of the letter.我正在寻找的最终结果是从数据库中提取样板字母,提取需要特定输入的部分,从这些部分生成表单,然后表单返回用户数据,并使用重建字母用户数据集成到信件的文本中。

for example, the string pulled from the database would look like this例如,从数据库中提取的字符串如下所示

Claim #: |string^clmNum^Claim Number: | - Ref#: |string^RefNum^Reference Number: |

and would end up like the following after it was rebuilt with user data:并在使用用户数据重建后最终如下所示:

Claim #: 123456 - Ref#: 789012

This is what I have working so far...这就是我到目前为止所做的工作......

The sections between the | |之间的部分are pulled out, split, and loaded into an IEnumerable被拉出、拆分并加载到 IEnumerable 中

My foo model is:我的 foo model 是:

public class Foo
{
   public string InputType {get; set;}
   public string InputName {get; set;}
   public string InputLabel {get; set;}
}

I pass the IEnumerable to the view with a ViewModel我使用 ViewModel 将 IEnumerable 传递给视图

public class FormBuildViewModel
{

   public IEnumerable<Foo> FooProperty {get; set;}

}

I then display the input items dynamically with the following Razor markup on my view.然后,我在视图中使用以下 Razor 标记动态显示输入项。

<form>
@{ var e = Model.FooProperty.ToList();


    foreach (var subItem in e)
    {
       <div class="FormGroup-items">
         <label>@subItem.InputLabel</label>
         <input name="@subItem.ObjName" type="text" />
       </div>
    }
 }
<..// form button stuff //..>
</form>

Which creates the following HTML:这将创建以下 HTML:

<form>
    <div class="FormGroup-items">
        <label>Claim Number: </label>
        <input name="clmNum" type="text" />
    </div>
    <div class="FormGroup-items">
        <label>Reference Number: </label>
        <input name="RefNum" type="text" />
    </div>

    <..// button stuff //..>
</form>

I have everything working up to this point.到目前为止,我已经完成了所有工作。 I need to take the data entered on the dynamically created form and get it back to the controller in a manner that I can index to rebuild the string.我需要获取在动态创建的表单上输入的数据并将其返回到 controller,以便我可以索引以重建字符串。

I've tried using the @html.beginform similar to this我试过使用类似于这个的@html.beginform

@using (Html.BeginForm())
{
    @for(int i=0; i<Model.Count; i++)
    {
       @Html.CheckBoxFor(m => m[i].IsActive, new { @value = Model[i].Id })
       @Html.DisplayFor(m => m[i].Name)
    }
    <input type="submit" value="Save" />
}

but to use @Html.BeginForm you need to know the names of the items before runtime, and it doesn't seem to work with a dynamically created form like this.但是要使用@Html.BeginForm,您需要在运行前知道项目的名称,而且它似乎不适用于像这样动态创建的表单。

The only thing I can think of is I need to load the form data into a List< T > and return that to the controller, but I can't think of a way to get C# to allow me to initialize a List< T > and load the values in the view.我唯一能想到的是我需要将表单数据加载到 List<T> 并将其返回到 controller,但我想不出一种方法来让 C# 允许我初始化 List<T>并加载视图中的值。 I know I've got to be missing something, but I'm kinda lost at this point.我知道我一定会错过一些东西,但我现在有点迷失了。 Any help would be appreciated.任何帮助,将不胜感激。

Are you passing your viewmodel back to your page?您是否将视图模型传递回您的页面? This seems like you are setting the viewmodel with data atleast from a 5000 foot view:这似乎您正在使用至少来自 5000 英尺视图的数据设置视图模型:

[HttpGet]
public IActionResult MyCallMethod()
{
    FooProperty = getmydatafromsomewhere();

    return View(); 
}

Then your page would have a way to build appropriately然后您的页面将有一种适当构建的方法

@model My.Name.Space.MyViewModel

@using (Html.BeginForm("Action", "Controller"))
{
    @foreach (var item in @Model.FooProperty)
    {
    <div class="FormGroup-items">
        <label asp-for="item.InputType" />
        <input asp-for="item.InputType" class="form-control" />
    </div>
    //other data

    }
}

I also assume you have a post setup on the controller.我还假设您在 controller 上进行了后期设置。

[HttpPost]
public IActionResult MyCallMethod(MyViewModel viewModel)
{
        //do something with the viewmodel here
        //same page, somewhere else, do something else, etc.
}

You can use some tag helpers as well for your labels and inputs if you so chose:如果您愿意,您也可以将一些标签助手用于您的标签和输入:

@Html.LabelFor(m => item.InputType, new { @class="whateverIwant" })
@Html.TextBoxFor(m => item.InputType, new { @class="form-control" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM