简体   繁体   English

在控制器MVC C#中生成动态的局部视图

[英]Generate a dynamic partialview within the controller MVC C#

I need to generate a dynamic form using MVC. 我需要使用MVC生成动态表单。
I'm planning to create the HTML code within the Action of a Controller(based on database values) and pass it to the View as a Partialview. 我打算在Controller的Action(基于数据库值)中创建HTML代码,并将其作为Partialview传递给View。
To accomplish this task I need to create a dynamic view inside the Action. 为了完成此任务,我需要在Action内部创建一个动态视图。 So I could add all the html codes within that and pass it to the view as a Partialview. 因此,我可以在其中添加所有html代码,并将其作为Partialview传递给视图。
Is there any way to do that ? 有什么办法吗?

Or 要么
Is there any other way in MVC to dynamically generate html elements ? MVC中还有其他方法可以动态生成html元素吗?

In that action, you can return html contents created on the fly. 在该操作中,您可以返回动态创建的html内容。 Just use Content() as bellows 只需使用Content()作为波纹管

return Content("<form><input value='"+valueFromDb+"'/>...</form>");

but this is not a good practice so, you need to create a partial view and then return it using 但这不是一个好习惯,因此,您需要创建一个局部视图,然后使用

//var modelFromDb=db.Table.FirstOrDefault();
 return PartialView("MyPartial",modelFromDb);

EDITS: To generate code for your partial, you need to right click in your controller action and choose Add View type MyPartial as View name and then check the Add Strongly Typed View ... you will need to precise the model class and the template to be used (list,delete,edit,create or empty) 编辑:要为您的局部生成代码,您需要右键单击控制器动作,然后选择“ 添加视图类型MyPartial作为视图名称”,然后选中“ 添加强类型视图” ...您将需要精确的模型类和模板以被使用(列表,删除,编辑,创建或为空)

I don't think generating code in controller is a good idea, things will get messy. 我认为在控制器中生成代码不是一个好主意,事情会变得一团糟。

If you are saving the fields in the database, you must be saving a type against each field. 如果要将字段保存在数据库中,则必须针对每个字段保存类型。

Example: 例:

Field Types Table

TypeId      Name
1           TextBox
2           DropDown


Fields Table

FieldId        TypeId (FK)    IsEdit    FormId (FK)       Style
1                1               1          1            color: red
2                2               1          1              NULL 

When reading from database you can convert these to a list of fields 从数据库中读取时,您可以将它们转换为字段列表

public class Field
{
// Common Properties
public string Style {get;set;}
public int    TypeId {get;set;}
public string Name {get;set;

}

then for each type you have in DB you can create a class and derive from Field 然后针对数据库中的每种类型,您可以创建一个类并从Field派生

public class DropDown : Field
{
    public Dictionary<string, string> Values {get;set;}
}

public class TextBox : Field
{
    public string Value {get;set;}
}

Then when reading from database pass each through a Factory class 然后,当从数据库中读取时,每个都通过Factory类

// READ from DB //从数据库读取

var model = new List<Field>();

var fields = dbContext.Fields.Where(s => s.FormId == 1);

foreach(var field in fields){

model.Add(FieldFactory(field));

}

// PASS THROUGH FACTORY //通过工厂

public Field FieldFactory(Form form){

switch(field.TypeId)
{
     case 1: // Textbox --> you can use enum
     return new TextBox(SET_VALUES);

}

}

Then create a partial View eg form.cshtml , pass list of field as model 然后创建一个局部视图,例如form.cshtml ,将字段列表作为模型传递

return PartialView("form", new List<Field>());

Then handle the field type in form.cshtml 然后在form.cshtml处理字段类型

@model List<Field>

@foreach(var field in Model){

switch(field.TypeId){

case 1: // TextBox
@Html.Partial("TextBox", (TextBox)field)
break;

case 2:
@Html.Partial("DropDown", (DropDown)field)
break;


}
}

Create a partial view for each field type: 为每种字段类型创建一个局部视图:

TextBox.cshtml: TextBox.cshtml:

@model TextBox

<input type ="text" name = "@Model.Name" stlye ="@Model.Style" value = "@Model.Value" />

DropDown.cshtml DropDown.cshtml

@model DropDown

@Html.DropDownListFor(m => m.FieldId, new SelectList(Model.Values, "Value", "Key")

With this method you add JavaScript and jQuery code to each field PartialView. 使用此方法,您可以将JavaScript和jQuery代码添加到每个字段PartialView。

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

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