ASP.NET MVC DropDownList,View,Controler和两个模型

时间:2015-01-19 12:04:13

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我已经为下面的模型,视图和控制器提供了代码。问题是:

我在数据库中有两个表,其中一个表示国家名称及其描述(国家表),另一个表格提供有关城市的信息(城市表)。

用于向City表添加新城市的表单需要有一个DropDownList,从中可以选择从Country传送的国家/地区,然后手动填写表单中的剩余数据并将其保存到City表。

型号:



namespace HarcodeASP.NETMVC5.Models
{
    public class HarcodeCountry
    {
        public int id { get; set; }
        [Required]
        [StringLength(250)]
        public string Title { get; set; }
        [Required]
        [DataType(DataType.MultilineText)]
        public string Section { get; set; }
        public string Time { get; set; }
        public string UserAdd { get; set; }
        public string UserEdit { get; set; }
    }

    public class HarcodeCity
    {
        public int id { get; set; }
        [Required]
        public string Country { get; set; }
        [Required]
        [StringLength(250)]
        public string Title { get; set; }
        [Required]
        [StringLength(15)]
        [DataType(DataType.PostalCode)]
        public string Zip { get; set; }
        public string Time { get; set; }
        public string UserAdd { get; set; }
        public string UserEdit { get; set; }
    }

...

   public class HarcodeDbContext : DbContext
    {

        public DbSet<HarcodeCountry> CountryDataset { get; set; }
        public DbSet<HarcodeCity> CityDataset { get; set; }
        public DbSet<HarcodeManufactor> ManufactorDataset { get; set; }
        public DbSet<HarcodeGroup> GroupDataset { get; set; }
        public DbSet<HarcodeSubgroup> SubgroupDataset { get; set; }
        public DbSet<HarcodeItem> ItemDataset { get; set; }
        public DbSet<HarcodeGallery> GalleryDataset { get; set; }
        public DbSet<HarcodeAudio> AudioDataset { get; set; }
        public DbSet<HarcodeVideo> VideoDataset { get; set; }
        public DbSet<HarcodePdf> PdfDataset { get; set; }
        public DbSet<ImgPath> ImgPathDataset { get; set; }

    }
}
&#13;
&#13;
&#13;

查看:

&#13;
&#13;
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("CityList", null, new { @class = "form-control", name = "HarcodeCountry.Title", id = "HarcodeCountry_Title" })
                
                @*@Html.EditorFor(model => model.HarcodeCountry.Title, new { htmlAttributes = new { @class = "form-control" } })*@
                @Html.ValidationMessageFor(model => model.Country, "", new { @class = "text-danger" })
            </div>
        </div>
        
        ...

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10 btn-group">
                <input type="submit" value="Create" class="btn btn-primary" />
                @Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-default" })
            </div>
        </div>
    </div>
}
&#13;
&#13;
&#13;

控制器:

&#13;
&#13;
public class CityController : Controller
    {
        private HarcodeDbContext db = new HarcodeDbContext();

        ... 

        // GET: Citie/Create
        public ActionResult Create()
        {
            var CountryQry = from d in db.CountryDataset orderby d.Title select d.Title;
            var CountryList = new List<string>();

            CountryList.AddRange(CountryQry);
            ViewBag.CityList = new SelectList(CountryList);

            return View();
        }

        // POST: Citie/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "id,Country,Title,Zip,Time,UserAdd,UserEdit")] HarcodeCity harcodeCity)
        {
            if (ModelState.IsValid)
            {
                harcodeCity.Time = DateTime.Now.ToString();
                harcodeCity.UserAdd = User.Identity.Name.ToString();
                harcodeCity.UserEdit = User.Identity.Name.ToString();

                db.CityDataset.Add(harcodeCity);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(harcodeCity);
        }

        ...

    }

       
&#13;
&#13;
&#13;

0 个答案:

没有答案