0

我尝试制作和级联下拉列表:第一个用于项目的 dropdwonlist,第二个用于活动的下拉列表。

型号: 项目:

usinng System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace ManyToMany.Models
{

    public enum StatusProject
    {
        Ja,
        Nee
    }

    public class Project
    {
        //[DatabaseGenerated(DatabaseGeneratedOption.None)]
        // [Display(Name= "Number")]
        [Key]
        public int ProjectID { get; set; }
        // [Display(Name = "Naam")]
        //[Required(ErrorMessage = "Naam moet wel ingevuld worden")]
        public string Name { get; set; }
        // [StringLength(100,ErrorMessage = "U kunt niet meer dan 100 tekens invoeren!! ")]
        // [Required(ErrorMessage = "Korte beschrijving")]
        [Display(Name ="omschrijving" )]
        public string Description { get; set; }

        // [Required(ErrorMessage = "U moet wel het aantal uren opgeven")]
        [Display(Name = "Geplande uren")]
        public decimal PlannedHours { get; set; }
        [Required(ErrorMessage = "eh, prijskaartje?")]
        [DisplayFormat(DataFormatString = "{0:c}")]
        [Display(Name = "Prijs")]
        public decimal price { get; set; }
        public bool Intern { get; set; }
        [Display(Name = "project manager")]
        public string projectManager { get; set; }
        [Display(Name = "Project Code")]
        public string projectCode { get; set; }
        [Display(Name = "Start Datum")]
        public DateTime? StartDate { get; set; }
        [Display(Name = "Eind datum")]
        public DateTime? EndDate { get; set; }
        [Display(Name = "Actief?")]
        public bool Active { get; set; }
        // public StatusProject StatusProject { get; set; }

        public virtual ICollection<Employee> Employees { get; set;}
        public virtual IEnumerable<Activity>Acitivity  { get; set; }
        //public IEnumerable<Activity> Activities { get; set; }
        //public IEnumerable<SelectListItem> Activities { get; set; }

        public Project()
        {
            this.Employees = new List<Employee>();
            this.Acitivity = new List<Activity>();

        }
    }
}

模型活动:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace ManyToMany.Models
{
    public class Activity
    {
        //[Key]
        //[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        //[Display(Name = "Nummer(willekeurig nummer)")]
        [Key]
        public int ActivityID { get; set; }
        [Required(ErrorMessage = "Naam moet wel ingevuld worden")]
        [MaxLength(50)]
        [Display(Name = "Activiteit")]
        public string Name { get; set; }

        [Display(Name = "Omschrijving")]
        public string Description { get; set; }

        [Display(Name = "uren")]
        public decimal Hour { get; set; }

        [DataType(DataType.Date)]
        [Display(Name = "Begin datum")]
        public DateTime BeginDate { get; set; }        

        [DataType(DataType.Date)]
        [Display(Name = "Eind datum")]
        public DateTime EndDate { get; set; }

        [Display(Name = "Tarief")]
        public decimal Tariff { get; set; }

        [Display(Name = "Actief?")]
        public bool Active { get; set; }

        [Display(Name = "project")]
        public int ProjectID { get; set; }
        //public virtual DbSet<Project> Project { get; set; }
        public virtual Project Project { get; set; }
        public virtual ICollection<Employee> Employee { get; set; }
        //public virtual ICollection<Activity> Activities { get; set; }
        public virtual ICollection<Hour> Hours { get; set; }
    }
}

看法:

@using System.Collections
@using System.Globalization
@using System.Web.Mvc.Html
@using ManyToMany.Helpers
@using ManyToMany.Controllers
@using ManyToMany.Models


@model ManyToMany.Models.Hour 
@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/SelectWeek.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/JQueryFixes.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.GetActivities.js")" type="text/javascript"></script>


<script type="text/javascript">
    $(document).ready(function () {
        $("#ProjectID").change(function () {
            var idDept = $(this).val();
            $.getJSON('@Url.Action("GetProjectList", "Hour")', { id: idDept },
                    function (myData) {
                        var select = $("#activity");
                        select.empty();

                        $.each(myData, function (index, itemData) {
                            select.append($('<option/>', {
                                value: itemData.Value,
                                text: itemData.Text
                            }));
                        });
                    });
                });
         });
    $(document).ready(function() {
        $("#ProjectID").change();
    });
</script>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Hour</legend>
        <table style="width: 10%;">
            <tr>
                <td>
                    <div class="editor-label">
                        @Html.LabelFor(model=> model.Activity.ProjectID,"Project")
                    </div>
                </td>
                <td>
                     <div class="editor-field">
                         @Html.DropDownListFor(model => model.Activity.Project.ProjectID, new SelectList(ViewBag.Projects as IEnumerable,"ProjectID","Name"),null, new {id ="ProjectID"}) 
                         @Html.ValidationMessageFor(model=> model.Activity.Project.Name)
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    <div class="editor-label">

                        @Html.LabelFor(model=> model.Activity.Name)
                    </div>
                </td>

                <td>
                    <div class="editor-field">
                        @Html.DropDownListFor(model => model.Activity, new SelectList(Enumerable.Empty<SelectListItem>(),"ActivityID", "Name"),null, new {id = "activity"}) 
                        @Html.ValidationMessageFor(model=> model.ActivityID)
                    </div>
                </td>
            </tr>
        </table>
        <br/>
        <table style="width: 100%;">
            <tr>
                <td>

                    <div class="editor-field">
                        @Html.Hidden("WeekID", String.Empty)
                        @Html.ValidationMessageFor(model => model.WeekID)
                    </div>
                </td>
                <td>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.WeekNumber, "week")
                    </div>
                    <div class="editor-field">
                        @Html.DropDownList("weeknr",(IEnumerable<SelectListItem>)Html.GetWeekNumbers(), new { onchange = "Selectedchange();" })
                        @Html.Hidden("weeknr", Html.GetWeekNumbers())
                        @Html.ValidationMessage("weeknr")
                    </div>
                </td>
                <td>
                    <div class="editor-label">
                        @Html.Label("Maandag")
                    </div>
                    <div class="editor-field">
                        @string.Format(new System.Globalization.CultureInfo("en-US").ToString())
                        @Html.TextBox("Monday", Model.Monday.ToString(), new{@readonly = "readonly" }) 
                       @Html.ValidationMessageFor(model=> model.Monday)
                    </div>
                </td>
                <td>
                    <div class="editor-label">
                        @Html.Label("Dindsdag")
                    </div>
                    <div class="editor-field">
                         @string.Format(new System.Globalization.CultureInfo("en-US"), "{0:0.##}", Model.Tuesday)
                        @Html.TextBox("Tuesday", Model.Tuesday.ToString(), new{@readonly = "readonly" }) 
                        @Html.ValidationMessageFor(model=> model.Tuesday)
                    </div>
                </td>
                <td>
                    <div class="editor-label">
                        @Html.Label("woendag")
                    </div>
                    <div class="editor-field">
                         @string.Format(new System.Globalization.CultureInfo("en-US"), "{0:0.##}", Model.Wendsday)
                        @Html.TextBox("Wendsday", Model.Wendsday.ToString(), new { @readonly = "readonly" }) 
                        @Html.ValidationMessageFor(model=> model.Wendsday)
                    </div>
                </td>
                <td>
                    <div class="editor-label">
                        @Html.Label("donderdag")
                    </div>
                    <div class="editor-field">
                         @string.Format(new System.Globalization.CultureInfo("en-US"), "{0:0.##}", Model.Thursday)
                        @Html.TextBox("Thursday", Model.Thursday.ToString(), new{@readonly = "readonly" }) 
                        @Html.ValidationMessageFor(model=> model.Thursday)
                    </div>
                </td>
                <td>
                    <div class="editor-label">
                        @Html.Label("vrijdag")
                    </div>
                    <div class="editor-field">
                         @string.Format(new System.Globalization.CultureInfo("en-US"), "{0:0.##}", Model.Fryday)
                        @Html.TextBox("Fryday", Model.Fryday.ToString(), new{@readonly = "readonly" }) 
                        @Html.ValidationMessageFor(model=> model.Fryday)
                    </div>
                </td>
                <td>
                    <div class="editor-label">
                        @Html.Label("Zaterdag")
                    </div>
                    <div class="editor-field">
                         @string.Format(new System.Globalization.CultureInfo("en-US"), "{0:0.##}", Model.Saterday)
                        @Html.TextBox("Saterday", Model.Saterday.ToString(), new{@readonly = "readonly" }) 
                        @Html.ValidationMessageFor(model=> model.Saterday)
                    </div>
                </td>
                <td>
                    <div class="editor-label">
                        @Html.Label("zondag")
                    </div>
                    <div class="editor-field">
                         @string.Format(new System.Globalization.CultureInfo("en-US"), "{0:0.##}", Model.Sunday)
                        @Html.TextBox("Sunday", Model.Sunday.ToString(), new{@readonly = "readonly" }) 
                        @Html.ValidationMessageFor(model=> model.Sunday)
                    </div>
                </td>
                <td>Totaal:</td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                 Uren:
                </td>
                <td>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.MondayHours)
                        @Html.ValidationMessageFor(model => model.MondayHours)
                    </div>
                </td>
                <td>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.TuesdayHours)
                        @Html.ValidationMessageFor(model => model.TuesdayHours)
                    </div>
                </td>
                <td>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.WendsdayHours)
                        @Html.ValidationMessageFor(model => model.WendsdayHours)
                    </div>
                </td>
                <td>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.ThursdayHours)
                        @Html.ValidationMessageFor(model => model.ThursdayHours)
                    </div>
                </td>
                <td>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.FrydayHours)
                        @Html.ValidationMessageFor(model => model.FrydayHours)
                    </div>
                </td>
                <td>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.SaterdayHours)
                        @Html.ValidationMessageFor(model => model.SaterdayHours)
                    </div>
                </td>
                <td>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.SundayHOurs)
                        @Html.ValidationMessageFor(model => model.SundayHOurs)
                    </div>
                </td>
                 <td> 
                    <div class="editor-field">
                        @Html.TextBox("0.0",Model.HourTotal, new {@readonly  = "readonly"} )
                        @Html.ValidationMessageFor(model => model.HourTotal)
                    </div>
                </td>
            </tr>
            <tr>
            </tr>
        </table>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

和控制器:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ManyToMany.Models;

namespace ManyToMany.Controllers
{ 

    //[Authorize(Roles = "Administrator")]
    public class HourController : Controller
    {
        private TimeSheetContext db = new TimeSheetContext();
        //
        // GET: /Hour/

        public ActionResult Index()
        {
            var Hours = db.Hours.Include(a => a.Activity).Include(a => a.Activity.Project);
            return View(db.Hours.ToList());
        }

        [HttpPost]
        public  ActionResult Index(int year)
        {
            return new EmptyResult();
        }

        //
        // GET: /Hour/Details/5

        public ViewResult Details(int id)
        {
            Hour hour = db.Hours.Find(id);
            return View(hour);
        }

        //
        // GET: /Hour/Create
        //Just getting the page from URL.Getting the data(Raw data).

        public ActionResult Create()
        {
            ViewBag.HourID = new SelectList(db.Hours, "HourID", "HourID");
            ViewBag.Projects = db.Projects.ToList();
            ViewBag.Activities = db.Activities.ToList();
            return View(new Hour());
        }//end method create 

        //[HttpPost]
        //Restricting an action: u also can write: [httpPost]. But then the second dropdownlist doesent work
        //[AcceptVerbs(HttpVerbs.Post)]
        [AcceptVerbs( HttpVerbs.Get)]
        public  JsonResult GetProjectList(string id)
        {
            var ProjectList = this.GetProjects(Convert.ToInt32(id));
            //DropdownList always have to get an text and Value
            var myData = ProjectList.Select(p => new SelectListItem
                                                     {
                                                         Text = p.Name,
                                                         Value = p.ActivityID.ToString(CultureInfo.InvariantCulture)
                                                     });
           //JSonRequestBehavior is an security issur. It is protecting against invalid data.
            return Json(myData, JsonRequestBehavior.AllowGet);
        }//end method

        //This is for the second dropdownlist: the activities. When a project is selected. The Id of the selected project
        //correspond with the associated Activities.
        private  IEnumerable<Activity> GetProjects(int id)
        {
            //List of activities corresponding with the selected Project
            return db.Activities.Where(p => p.ProjectID == id).ToList();

        }//end method

            //
        // POST: /Hour/Create

        [HttpPost]
        public ActionResult Create(Hour hour)
        {
            Activity temp = new Activity();
            temp = GetProjects(1).FirstOrDefault();
            hour.Activity = temp;
            hour.ActivityID = temp.ActivityID;

            try
            {
                if (ModelState.IsValid)
                {
                    if (db.Hours != null) db.Hours.Add(hour);

                    //db.Hours.Add(hour.Monday.Value.ToString("d"));

                    db.SaveChanges();
                    //ViewBag.Projects = db.Projects.ToList();
                    //ViewBag.Activities = db.Activities.ToList();
                    return RedirectToAction("Index");
                }
            }
            catch (Exception exception)
            {
                ModelState.AddModelError("", exception);
            }//end catch exception.

            //Important!! This will mangage that the actuall data will be saved.If u forget this then u only see it in the view!!

            ViewBag.Projects = db.Projects.ToList();
            ViewBag.Activities = db.Activities.ToList();
            return View(hour);
        }//end method

        //
        // GET: /Hour/Edit/5

        public ActionResult Edit(int id)
        {
            Hour hour = db.Hours.Find(id);
            ModelState.AddModelError("Name", "What a nice name");
            return View(hour);
        }//end method

        //
        // POST: /Hour/Edit/5

        [HttpPost]
        public ActionResult Edit(Hour hour)
        {
            if (ModelState.IsValid)
            {
                db.Entry(hour).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID", hour.WeekID);
            ViewBag.Projects = db.Projects.ToList();
            ViewBag.Activities = db.Activities.ToList();
            return View(hour);
        }//end method

        //
        // GET: /Hour/Delete/5

        public ActionResult Delete(int id, bool? saveChanges)
        {
            if (saveChanges.GetValueOrDefault())
            {
                ViewBag.ErrorMessage = "Cant save";
            }
            Hour hour = db.Hours.Find(id);
            return View(hour);
        }//end method

        //
        // POST: /Hour/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Hour hour = db.Hours.Find(id);
            db.Hours.Remove(hour);
            db.SaveChanges();
            return RedirectToAction("Index");
        }//end method

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }//end method
    }
}

这是关于Edit method(HttpPost). 你可以选择一个项目和相关的活动。但是提交到数据库是不可能的。因为我收到此错误:

ModelState.IsValid = false.

请问有人可以帮我吗??

4

5 回答 5

0

这是我的模型小时:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Globalization;
using System.Web.Mvc;

namespace ManyToMany.Models
{
    public class Hour
    {

        //public string Name { get; set; }
        //public string Url { get; set; }
        public int HourID { get; set; }
       // public virtual Week Week { get; set; }
        public int? WeekID { get; set; }
        //[Key]
        public int? ActivityID { get; set; }
        public virtual Activity Activity { get; set; }
        //public int ActivtityID { get; set; }






        //[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString="{0:c}")]
        //public decimal? Amount { get; set; }


        [ReadOnly(true)]
        //[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public decimal HourTotal { get { return GetTotalHours(); }   }



        public int WeekNumber { get; set; }
        public int yearNumber { get; set; }

        //Properties days:

        public DayOfWeek DayWeek { get; set; }

        //public DateTime DateDay { get; set; }


       [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public DateTime? Monday { get; set; }
        public int Days { get; set; }


        [DataType(DataType.Date)]//
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public DateTime? Tuesday { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public DateTime? Wendsday { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public DateTime? Thursday { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public DateTime? Fryday { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public DateTime? Saterday { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public DateTime? Sunday { get; set; }
        //End properties days


        //Properties hours:
       // [DisplayFormat(DataFormatString = "{0:#.##0.0#}", ApplyFormatInEditMode = true)]
        [Required]
        //[DisplayFormat(DataFormatString= "{0:#,##0.000#}")] 
        public Decimal MondayHours { get; set; }
        //[DisplayFormat(DataFormatString = "{0:#.#}", ApplyFormatInEditMode = true)]
        [Required]
        public Decimal TuesdayHours { get; set; }
        //[DisplayFormat(DataFormatString = "{0:#.#}", ApplyFormatInEditMode = true)]
        [Required]
        public Decimal WendsdayHours { get; set; }
        //[DisplayFormat(DataFormatString = "{0:#.#}", ApplyFormatInEditMode = true)]
        [Required]
        public Decimal ThursdayHours { get; set; }
        //[DisplayFormat(DataFormatString = "{0:#.#}", ApplyFormatInEditMode = true)]
        [Required]
        public Decimal FrydayHours { get; set; }
        //[DisplayFormat(DataFormatString = "{0:#.#}", ApplyFormatInEditMode = true)]
        [Required]
        public Decimal SaterdayHours { get; set; }
        //[DisplayFormat(DataFormatString = "{0:#.#}", ApplyFormatInEditMode = true)]
        [Required]
        public Decimal SundayHOurs { get; set; }

        public string TextMonday { get; set; }
        public string TextTuesday { get; set; }
        public string TextWendsday { get; set; }
        public string TextThursday { get; set; }
        public string TextFryday { get; set; }
        public string TextSaterday { get; set; }
        public string TextSunday { get; set; }





        public decimal GetTotalHours()
        {
            return MondayHours + 
                   TuesdayHours + 
                   WendsdayHours + 
                   ThursdayHours + 
                   FrydayHours + 
                   SaterdayHours +
                   SundayHOurs;
        }




        //public IEnumerable<SelectListItem> Hour { get; set; }



        public Hour()
        {

            //Activity = new List<Activity>();

            this.yearNumber = DateTime.Now.Year;

            Days = DateTime.Now.DayOfWeek - DayOfWeek.Monday;
            Monday = DateTime.Now.AddDays(-Days);
            Tuesday =  DateTime.Now.AddDays(0);// .Monday.AddDays(1).ToShortDateString();//.ToShortDateString());
            Wendsday = DateTime.Now.AddDays(1);  //DateTime.Parse(Monday.AddDays(2).ToShortDateString());
            Thursday = DateTime.Now.AddDays(2);    //DateTime.Parse(Monday.AddDays(3).ToShortDateString());
            Fryday = DateTime.Now.AddDays(3);          //DateTime.Parse(Monday.AddDays(4).ToShortDateString());
            Saterday = DateTime.Now.AddDays(4);              //DateTime.Parse(Monday.AddDays(5).ToShortDateString());
            Sunday =   DateTime.Now.AddDays(5);             //DateTime.Parse(Monday.AddDays(6).ToShortDateString());

            this.WeekNumber = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
        }
    }
}
于 2012-07-11T20:56:01.947 回答
0

这是因为您正在分配控制器中的所有活动。在控制器 [Get] 编辑操作中,从项目对象中分配 ViewBag 活动。但是我在小时模型中没有找到任何项目属性,我的意思是在保存时您将如何决定要节省的项目时间?..

get 方法上没有 Viewbag 分配,这是您必须按两次按钮的原因!

于 2012-07-12T12:13:27.373 回答
0

如果您的怀疑是正确的,那么我担心您在这里做错了什么......什么是活动类型?......

 <div class="editor-field">
                    @Html.DropDownListFor(model => model.Activity, new SelectList(Enumerable.Empty<SelectListItem>(),"ActivityID", "Name"),null, new {id = "activity"}) 
                    @Html.ValidationMessageFor(model=> model.ActivityID)
  </div> 

请告诉我你想用那段代码做什么。

直到现在我认为你应该表现得像......

 <div class="editor-field">
                    @Html.DropDownListFor(model => model.ActivityID, new SelectList(Model.Activities,"ActivityID", "Name"),null, new {id = "activity"}) 
                    @Html.ValidationMessageFor(model=> model.ActivityID)
  </div> 

在 dropdownFor 表达式中,表达式应该用于您要在其中发布该数据的字段。我的意思是,如果您编写 model => model.Activity,则该值将传递给 Activity,如果您编写 model => model.ActivityID,则该值将传递给 ActivityID。

乍一看,它看起来像那样..如果不是那样。请分享您的 Hour Modal 取决于哪个编辑视图。这样我们就可以准确地得出结论。

于 2012-07-11T06:40:39.100 回答
0

我收到了您的电子邮件,但我无法访问您的项目。对此感到抱歉。我正在努力尽快得到它。同时我发现您的问题是......

在 View SelectList 中应该绑定您要引用的活动。这就是为什么我在这里将其更改为 Activity.Project.Activity。如果您以其他方式曝光它,您可以根据您的曝光进行更改。

如果您只需要特定项目的活动,请使用此

 <div class="editor-field">
                    @Html.DropDownListFor(model => model.Activity.ActivityID, new SelectList(Model.Activity.Project.Activity as IEnumerable,"ActivityID","Name"),null, new {id ="Activity"})
                    @Html.ValidationMessageFor(model=> model.ActivityID)
 </div> 

现在,您的 get [GET] Edit 方法不应该提供任何数据,因为小时模型将提供活动列表。如果您想传递模型外的另一个活动集合,则传入 Viewbag。然后你的小时控制器 [GET] 编辑方法将如下所示......

 public ActionResult Edit(int id)
        {
            Hour hour = db.Hours.Find(id);
            ModelState.AddModelError("Name", "What a nice name");
            return View(hour);
        }

如果您对模型以外的绑定进行了额外的练习,那么对于 post 方法也是如此,那么如果您有相同的视图作为响应,则必须重复它。post 方法看起来像这样....

    [HttpPost]
    public ActionResult Edit(Hour hour)
    {
        if (ModelState.IsValid)
        {
            db.Entry(hour).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID", hour.WeekID);
        ViewBag.Projects = db.Projects.ToList();
        ViewBag.Activities = db.Activities.ToList();
        return View(hour);
    }

这将解决您的项目特定活动问题和两次发布问题可用的绑定。在我可以访问您的项目之前,请发布您的进一步问题,以便我可以在这里告诉您。当 ViewModelState 说有任何错误时,它也会说该属性,因此很容易解决。

于 2012-07-16T06:49:44.760 回答
0

好的.....所以用这个替换你的活动下拉列表......

 <div class="editor-field">
                    @Html.DropDownListFor(model => model.Activity.ActivityID, new SelectList(ViewBag.Activities as IEnumerable,"ActivityID","Name"),null, new {id ="Activity"})
                    @Html.ValidationMessageFor(model=> model.ActivityID)
 </div> 

您不是在 Hour 模型中而是在 Activity 模型中收到 ActivityID null 错误。据说它是一个键,因此在绑定模型时它不能为空。上面的代码将绑定它,我希望你不会在 ModelState 中得到任何错误。

如果您遇到任何进一步的绑定错误,请让我知道 ModelState 中的哪个属性抛出错误。

于 2012-07-12T07:56:20.613 回答