在C#类中生成razor语法并发送到视图

在C#类中生成razor语法并发送到视图,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我有一门课叫做“评分”,有5种不同的评分(1=差..5=优秀)。我还有一个模型(回顾),其中有10个问题,每个问题都使用评级类。现在在视图中,我对Review类中的每个属性都有一个forEach,因此代码有点像剪贴一样。如果可能的话,我想在Ratings类中创建一个生成razor语法的方法,而不是复制代码并更改它们的属性 下面是关于现在的情况和我想做什么的示例 当前视图(仅显示2个属性: <tr> <td class="control-label col-md-4"&g

我有一门课叫做“评分”,有5种不同的评分(1=差..5=优秀)。我还有一个模型(回顾),其中有10个问题,每个问题都使用评级类。现在在视图中,我对Review类中的每个属性都有一个forEach,因此代码有点像剪贴一样。如果可能的话,我想在Ratings类中创建一个生成razor语法的方法,而不是复制代码并更改它们的属性

下面是关于现在的情况和我想做什么的示例

当前视图(仅显示2个属性:

<tr>
    <td class="control-label col-md-4">
        @Html.LabelFor(model => model.ReviewModel.SpeakerReview, htmlAttributes: new { @class = "control-label " })
    </td>
    @foreach (var rating in ratingModel.RatingList)
    {
        <td class="col-md-1">
            @Html.RadioButtonFor(model => model.ReviewModel.SpeakerReview, rating.RatingId)
        </td>
    }
</tr>
<tr>
    <td class="control-label col-md-4">
        @Html.LabelFor(model => model.ReviewModel.AvHandoutsApplicable, htmlAttributes: new { @class = "control-label " })
    </td>
    @foreach (var rating in ratingModel.RatingList)
    {
        <td class="col-md-1">
            @Html.RadioButtonFor(model => model.ReviewModel.AvHandoutsApplicable, rating.RatingId)
        </td>
    }
</tr>

@LabelFor(model=>model.ReviewModel.speakerview,htmlAttributes:new{@class=“control label”})
@foreach(评级模型中的var评级。评级列表)
{
@Html.radioButton(model=>model.ReviewModel.speakerView,rating.RatingId)
}
@LabelFor(model=>model.ReviewModel.AvHandoutsApplicable,htmlAttributes:new{@class=“controllabel”})
@foreach(评级模型中的var评级。评级列表)
{
@Html.radioButton(model=>model.ReviewModel.AvHandoutsApplicable,rating.RatingId)
}
我希望它看起来怎么样:

视图:


@LabelFor(model=>model.ReviewModel.speakerview,htmlAttributes:new{@class=“control label”})
@ReviewModel.BuildRatingList(评级模型,ReviewModel.SpeakerView);
@LabelFor(model=>model.ReviewModel.AvHandoutsApplicable,htmlAttributes:new{@class=“controllabel”})
@ReviewModel.BuildRatingList(ratingModel,ReviewModel.AvHandoutsApplicable);
类别:

public static string BuildRatingList(Rating ratingModel, object reviewItem)
{
    string RtnVal = "";
    foreach (var rating in ratingModel.RatingList)
    {
        RtnVal = "<td class='col-md-1'>@Html.RadioButtonFor(model => " + reviewItem + ", rating.RatingId)</td>";
    }
    return RtnVal;
}
publicstaticstringbuildratinglist(评级模型,objectreviewItem)
{
字符串RtnVal=“”;
foreach(评级模型中的var评级。评级列表)
{
RtnVal=“@Html.radiobutton(model=>”+reviewItem+”,rating.RatingId)”;
}
返回RtnVal;
}

提前感谢!

虽然您可以通过从NUGET插入额外模块来完成几乎完全符合您要求的操作,但我认为您最好使用普通模块或组件,例如:

namespace ViewComponentSample.ViewComponents
{
    public class PriorityList : ViewComponent
    {
        private readonly ToDoContext db;

        public PriorityList(ToDoContext context)
        {
            db = context;
        }

        public async Task<IViewComponentResult> InvokeAsync(
        int maxPriority, bool isDone)
        {
            var items = await GetItemsAsync(maxPriority, isDone);
            return View(items);
        }
        private Task<List<TodoItem>> GetItemsAsync(int maxPriority, bool isDone)
        {
            return db.ToDo
                     .Where(x => x.IsDone == isDone && x.Priority <= maxPriority)
                     .ToListAsync();
        }
    }
}
命名空间ViewComponentSample.ViewComponents
{
公共类优先级列表:ViewComponent
{
私有只读ToDoContext数据库;
公共优先级列表(ToDoContext上下文)
{
db=上下文;
}
公共异步任务InvokeAsync(
int maxPriority,bool isDone)
{
var items=await-GetItemsAsync(maxPriority,isDone);
返回视图(项目);
}
私有任务getItemsAsSync(int-maxPriority,bool-isDone)
{
返回db.ToDo

.Where(x=>x.IsDone==IsDone&&x.Priority您是否尝试使用部分视图?正在努力将对象传递/读取到部分视图并读取它。例如,如何传递和读取“model.ReviewModel.speakerview”,在简单情况下,它将是
@Html.partial(“speakerviewpartial”,model.ReviewModel.speakerview)
我确实做到了这一点,但在局部视图中遇到了问题。这有点像新手。也许这会对你有所帮助。谢谢Vittore,选择局部视图。
namespace ViewComponentSample.ViewComponents
{
    public class PriorityList : ViewComponent
    {
        private readonly ToDoContext db;

        public PriorityList(ToDoContext context)
        {
            db = context;
        }

        public async Task<IViewComponentResult> InvokeAsync(
        int maxPriority, bool isDone)
        {
            var items = await GetItemsAsync(maxPriority, isDone);
            return View(items);
        }
        private Task<List<TodoItem>> GetItemsAsync(int maxPriority, bool isDone)
        {
            return db.ToDo
                     .Where(x => x.IsDone == isDone && x.Priority <= maxPriority)
                     .ToListAsync();
        }
    }
}
@using ViewComponentSample.Models
@using ViewComponentSample.ViewComponents
@model IEnumerable<TodoItem>

<h2>ToDo nameof</h2>

<div>
    @await Component.InvokeAsync(nameof(PriorityList), 
                      new { maxPriority = 4, isDone = true })
</div>