MVC 网站中的动态选项

发布于 2024-10-12 13:49:38 字数 285 浏览 1 评论 0原文

我有一个包含产品详细信息视图的 MVC 网站。 此视图上有有关该产品的许多详细信息。

我需要的是在用户将商品添加到购物车之前询问一些问题。 例如什么颜色、什么尺寸、数量......但并非所有产品都有相同的选项列表。 某些选项将有一个下拉列表,其他选项将是自由文本字段。 构建页面的这一部分的最佳方法是什么?感觉就像我需要产品表中的 xml 字段来保存特定于产品的 xml,可以将其解析到此页面的选项部分。但我不知道这是否可能,也不知道如何保存数据。

如果您对最佳解决方案有任何想法,我将非常感激!

谢谢 约翰

I have a MVC web site that has a Product details view.
On this view are a number of details about the product.

What i need is to ask the user a number of questions before they add the item to their cart.
for example what colour, what size, QTY.... but not all products have the same list of options.
Some options will have a dropdown list others will be free text field.
What is the best way to build this section of the page? it feels like i need a xml field in the product table that holds product specific xml that can be parsed into the options section of this page. But i dont know if that is even possible or how i would save the data.

If you have any ideas as to the best solution i would be very grateful !!

Thanks
John

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Oo萌小芽oO 2024-10-19 13:49:38

很简单。

首先,请查看模板文件夹<在 MVC 中,您有两种类型 DisplayTemplates 和 EditorTemplates。

现在它们的工作方式与普通视图相同,但位于特殊文件夹中,例如名称与上面相同。

\Views\Home\Index.cshtml  
\Views\Home\DisplayTemplates\MyViewModelName.cshtml
\Views\Home\EditorTemplates\MyViewModelName.cshtml

这就是他们的工作方式。

如果您像这样创建 ProductDetailsViewModel

public class ProductDetailsViewModel {
     public IList<Question> Questions { get;set; }
}

Question 可以是这样的抽象类:

public abstract class Question {
 // .. maybe need some common properties here, like answer value?
}

然后您可以定义更具体的问题,例如:

public class TextQuestion : Question {

}

public class RadioQuestion : Question {

 // maybe has a list of options for display etc.

}

等等。

然后假设在 \Home\Index.cshtml 视图中使用上述结构,您可以简单地执行以下操作。

@using(Html.BeginForm())
{
     <p>Put your form here</p>

     @Html.TextboxFor(x=>x.ProductName)

     @Html.EditorFor(x => x.Questions)

     <input type="submit" value="Submit" />
}

接下来发生的是 MAGIC,对于每个实际类型 TextQuestionRadioQuestion 它将在 EditTemplate 中查找相应的部分视图并根据需要进行渲染。

因此,如果我们将 \Home\EditorTemplates\RadioQuestion.cshtml 定义为:

@model RadioQuestion

<p>This is a radio button based question</p>

// Implement the radio buttons or text boxes etc here.

当然显示的工作方式相同。

Quite simply.

To start off, have a look at template folders in MVC, you get two types DisplayTemplates and EditorTemplates.

Now they work the same as a normal view, but live in special folders, named the same as above for example.

\Views\Home\Index.cshtml  
\Views\Home\DisplayTemplates\MyViewModelName.cshtml
\Views\Home\EditorTemplates\MyViewModelName.cshtml

This is how they work.

If you create your ProductDetailsViewModel like this:

public class ProductDetailsViewModel {
     public IList<Question> Questions { get;set; }
}

A Question can be an abstract class like this:

public abstract class Question {
 // .. maybe need some common properties here, like answer value?
}

Then you can define more specific questions like:

public class TextQuestion : Question {

}

public class RadioQuestion : Question {

 // maybe has a list of options for display etc.

}

And so on.

Then lets say using the above structure in you \Home\Index.cshtml view you could simply do the following.

@using(Html.BeginForm())
{
     <p>Put your form here</p>

     @Html.TextboxFor(x=>x.ProductName)

     @Html.EditorFor(x => x.Questions)

     <input type="submit" value="Submit" />
}

What will happen next is MAGIC, for each actual type TextQuestion, RadioQuestion it will look for the corresponding partial view in EditTemplate and render it as appropriate.

So if we have \Home\EditorTemplates\RadioQuestion.cshtml defined as:

@model RadioQuestion

<p>This is a radio button based question</p>

// Implement the radio buttons or text boxes etc here.

Of course display works in the same way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文