显示信息 - MVC 5
问题描述:
我有一个产品型号这样显示信息 - MVC 5
public class ProductViewModel
{
public int Id { get; set; }
public string Description { get; set; }
public bool IsActive { get; set; }
public ProductTypeFlag ProductType { get; set; }
public string BikeMake { get; set; }
public string BikeModel { get; set; }
public string CarMake { get; set; }
public string CarModel { get; set; }
public string TrainMake { get; set; }
public string TrainModel { get; set; }
}
public enum ProductTypeFlag
{
Bike = 0,
Car = 1,
Train = 2
}
正如你所看到的,我只有三个产品可供选择:自行车,汽车或火车。
我创建新的产品图片目前正在寻找这样的...在那里我有一个下拉列表中选择了ProductType
@model WebApplication14.Models.ProductViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>ProductViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.IsActive)
@Html.ValidationMessageFor(model => model.IsActive, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProductType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.ProductType, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProductType, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.BikeMake, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BikeMake, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BikeMake, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.BikeModel, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BikeModel, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BikeModel, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CarMake, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CarMake, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CarMake, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CarModel, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CarModel, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CarModel, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TrainMake, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TrainMake, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TrainMake, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TrainModel, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TrainModel, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TrainModel, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
现在,我想要什么要做的只是显示与选定产品相关的产品信息。例如,如果我选择Bike作为产品,那么我只想看到BikeMake和BikeModel可用 - 即我不希望看到Car/Train-Model &模型在那里,因为它是无关紧要的。
答
您可以将与每种车型相关的属性分组到容器格中,并根据下拉列表中的选择条件隐藏/显示。
例如
<div my-section="section-0" style="display:none;">
<div class="form-group">
@Html.LabelFor(model => model.BikeMake, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BikeMake, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.BikeMake)
</div>
</div>
</div>
<div my-section="section-1" style="display:none;">
<!-- Inputs for code car related fields goes here -->
</div>
<div my-section="section-2" style="display:none;">
<!-- Inputs for Train related fields goes here -->
</div>
现在听的变化事件的SELECT元素上只显示div容器。
$(function() {
// Show the section for the current(default) selection of the dropdown
var t = $("#ProductType").val();
var item = $("[my-section='section-" + t + "']").show();
// Wire up change event code for dropdown
$("#ProductType").change(function (e) {
// Hide all the sections (including previously shown)
$("[my-section]").hide();
//Select only the section corresponding the the dropdown selection
var item = $("[my-section='section-" + $(this).val() + "']").show();
})
});
例如,如果您在下拉列表中选择第二项,jQuery选择代码$("[my-section='section-" + $(this).val() + "']")
将返回DIV设置为"section-1"
谢谢您的帮助
my-section
属性值。有用。但是,我仍然在学习JQuery的基础知识。你能不能一行一行地解释脚本的作用? – CB4在代码中添加了一些注释,以便于在此行中理解: – Shyju
var t = $(“#ProductType”)。val(); jquery如何知道我选择的产品? #ProductType表示ID等于#ProductType的div吗?我没有那个。我知道你提到了一些关于违约的事情......但我并不完全明白这一点。 – CB4