سؤال

مؤخرا نشرت لي سؤال حول html مساعد dropdownlist وحصلت عليه العامل (هنا).لكن الآن قررت كان الكثير أكثر ذكاء للتبديل إلى ModelView أنماط لذلك يجب الق بقوة كتبته الطرق في وجهات نظري.... الخما فعلته هو أنني قمت ببعض التعديلات على قانون في الموضوع بالطريقة التالية:

VacatureFormViewModel:

public class VacaturesFormViewModel
{
    public Vacatures Vacature { get; private set; }
    public SelectList EducationLevels { get; private set; }
    public SelectList Branches { get; private set; }
    public SelectList CareerLevels { get; private set; }

    Repository repository;

    // Constructor
    public VacaturesFormViewModel(Vacatures vacature)
    {
        this.Vacature = vacature;
        this.repository = new Repository();
        this.EducationLevels = new SelectList(repository.GetAllEducationLevels(),"ID","Name",vacature.EducationLevels);
        this.Branches = new SelectList(repository.GetAllBranches(),"ID","Name",vacature.Branches);
        this.CareerLevels = new SelectList(repository.GetAllCareerLevels(), "ID", "Name", vacature.CareerLevels);

    }
}

BanenController:

//
    // GET: /Banen/Create

    public ActionResult Create()
    {
        Vacatures vacature = new Vacatures();
        return View(new VacaturesFormViewModel(vacature));
    }

    //
    // POST: /Banen/Create

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(Vacatures vacatureToAdd)
    {
        if (ModelState.IsValid)
        {
            try
            {
                // TODO: Add insert logic here
                repository.AddToVacatures(vacatureToAdd);
                repository.SaveChanges();

                // Return to listing page if succesful
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                return View();
            }
        }
    }

و خلق بلدي.aspx عرض (جزء منه):

<% using (Html.BeginForm()) {%>

    <fieldset>
        <legend>Fields</legend>
        <p>
            <label for="Title">Title:</label>
            <%= Html.TextBox("Title", Model.Vacature.Title) %>
            <%= Html.ValidationMessage("Title", "*") %>
        </p>
        <p>
            <label for="Content">Content:</label>
            <%= Html.TextArea("Content", Model.Vacature.Content) %>
            <%= Html.ValidationMessage("Content", "*") %>
        </p>
        <p>
            <label for="EducationLevels">EducationLevels:</label>
            <%= Html.DropDownList("EducationLevels", Model.EducationLevels)%>
            <%= Html.ValidationMessage("EducationLevels", "*") %>
        </p>
        <p>
            <label for="CareerLevels">CareerLevels:</label>
            <%= Html.DropDownList("CareerLevels", Model.CareerLevels)%>
            <%= Html.ValidationMessage("CareerLevels", "*")%>
        </p>
        <p>
            <label for="Branches">Branches:</label>
            <%= Html.DropDownList("Branches", Model.Branches)%>
            <%= Html.ValidationMessage("Branches", "*")%>
        </p>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
<% } %>

لتوجيه لقد استخدمت NerdDinner البرنامج التعليمي من قبل ScottGu و قرأت عدة مواضيع هنا.

سؤالي هو إذا كان من الممكن السماح MVC ASP تعيين careerlevel, educationlevel و الفروع (dropdownlists) تلقائيا كما هو عليه حاليا هو العودة هوية السلسلة التي ليس ما أريد.عندما أقوم بتغيير خلق SelectList إلى:

this.CareerLevels = new SelectList(repository.GetAllCareerLevels(), vacature.CareerLevels);

حتى من دون "هوية" و "اسم" لا يحفظ إما (أعتقد أنه لا يزال عاد كسلسلة في أسلوب آخر ، وليس هدف في حد ذاته) و بجانب هذا, وهو يسرد في عرض النحو التالي:vacature.EducationLevels.... الخحتى لا أسماء ولكن الكائن نفسه غير المدرجة.

السؤال الأخير لذا باختصار سؤالي هو إذا كان من الممكن استخدام هذا النهج في تعيين branche educationallevel و careerlevel.حتى لا تلقائيا ؟

في هذه الحالة أنا لا تزال بحاجة الى استخدام أشياء مثل:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection form)
    {
        Vacatures vacatureToAdd = new Vacatures();

        // Retrieve the education level by its ID
        if (!form["EducationLevels"].Equals(""))
        {
            Guid educationID = new Guid(form["EducationLevels"]);
            vacatureToAdd.EducationLevels = repository.GetEducationLevelByID(educationID);
        }

في بلدي تحكم ؟ أو هل هناك أي خيارات أكثر سلاسة.

هل كانت مفيدة؟

المحلول

تحريرها باستخدام Guid:

مع dropdownlists يمكنني استخدام نهج مختلف قليلا.فإنه قد يكون من الممكن الحصول على viewmodel العامل ، ولكن بالنسبة لي هو أسهل بهذه الطريقة:

public class VacaturesFormViewModel
{

    public IEnumerable<SelectListItem>EducationLevels{ get; set; }
    public Guid EducationLevelID{ get; set; }

}

على EducationLevelID سوف يكون اختيار معرف من القائمة المنسدلة.هذا هو القائل:

<%= Html.DropDownList("EducationLevelID", Model.EducationLevels)%>

تحكم

  IEnumerable<SelectListItem> educationLevelList =
                            from level in GetLevelList()
                            select new SelectListItem
                            {
                                Text = level .Name,
                                Value = level.Uid.ToString()
                            };
  model.EducationLevels = educationLevelList ;

نصائح أخرى

لست متأكد ولكن أعتقد أنه يجب عليك إنشاء نموذج المجلدات.(ديفيد هايدن كتب نموذج بسيط الموثق)

هل يمكن ربط educationID المعلمة تلقائيا:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Guid? educationID, FormCollection form)
{
    Vacatures vacatureToAdd = new Vacatures();

    if (educationID != null)
    {
        vacatureToAdd.EducationLevels = 
            repository.GetEducationLevelByID(educationID.Value);
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top