我要将另一个控制器的数据发送到另一个视图。 我有一个家庭控制器,但我不想发送到Home View数据,因为我需要在Car View中获取这些数据,我从数据库中获取下拉列表
Home COntroller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using Automarket.Models;
using System.ComponentModel.DataAnnotations;
using static System.Net.Mime.MediaTypeNames;
namespace Automarket.Controllers
{
public class HomeController : Controller
{
OurDBContext db = new OurDBContext();
// Controller for populate dropdownlist
// GET: Home
public ActionResult Index()
{
return View();
}
public ActionResult Price()
{
OurDBContext db = new OurDBContext();
ViewBag.price = new SelectList(db.prices, "Id", "price");
return View("Car");
}
}
}
汽车视图
@using Automarket.Models;
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
List<Marke> marke = ViewData["marke"] as List<Marke>;
List<Modeli> modeli = ViewData["modeli"] as List<Modeli>;
string deps = ViewData["deps"] as string;
}
<h2>Automarket</h2>
<div>
@Html.DropDownList("Prices" , "--Select Item--")
</div>
<div class="dropdown">
<select id="brand" onchange="popMod(@deps)" name="brand" class="form-control">
@foreach (var mrka in marke)
{
<option value="@mrka.Id">@mrka.Name</option>
}
</select>
</div>
<p>
<select id="modeli" name="model" class="form-control">
@foreach (var modell in modeli)
{
<option value="@modell.Id">@modell.Name</option>
}
</select>
</p>
<script>
function popMod(mo) {
var mod = document.getElementById("modeli");
var len = mod.length;
while (mod.options.length > 0) {
mod.remove(0);
}
var sel = document.getElementById("brand");
var id = sel.options[sel.selectedIndex].value;
for (el in mo) {
if (mo[el].MarkeId == id) {
var op=document.createElement("option");
op.value=mo[el].Id;
op.innerHTML=mo[el].Name;
mod.appendChild(op);
}
}
}
</script>