0

So basically once the user clicks on the module for create, I would want it to show the module shown instead of a drop down list for selection.


@model Module1.Models.Learn

@{
    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>

@using (Html.BeginForm()) {
  @Html.ValidationSummary(true)
  <fieldset>
    <legend>Learn</legend>
      <div class="editor-label">
        @Html.LabelFor(model => model.ModuleId, "Module") 
      </div>
      <div class="editor-field">
        @Html.DropDownList("ModuleId", String.Empty)
        @Html.ValidationMessageFor(model => model.ModuleId)
      </div>

Above is my Create View for Learning Outcome.


@model Module1.Models.Module

@{
    ViewBag.Title = "Details";
}

<h2>Learning Outcome : @Model.Code</h2>
<br /

This is the Details View for my Learning Outcome. I tried using the code "@Module.Code" to portray in my Create View but was unsuccessful. Is there another way for me to have the same result in the create view?


My Module Model

using System.Collections.Generic;

namespace Module1.Models
{
   public class Module
{
    public int ModuleId { get; set; }
    public int CourseId { get; set; }
    public int LecturerId { get; set; }
    public string Code { get; set; }
    public string Description { get; set; }

    public virtual Course Course { get; set; }
    public virtual Lecturer Lecturer { get; set; }

    public List<Reference> References { get; set; }
    public List<Assessment> Assessments { get; set; }//new
    public List<Front> Fronts { get; set; }
    public List<Learn> Learns { get; set; }
    public List<Topic> Topics { get; set; }

}
}

My Learn Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Module1.Models
{
    public class Learn
    {
    public int LearnId { get; set; }
    public int ModuleId { get; set; }
    public string LearnText { get; set; }
    public bool A { get; set; }
    public bool B { get; set; }
    public bool C { get; set; }
    public bool D { get; set; }
    public bool E { get; set; }
    public bool F { get; set; }
    public bool G { get; set; }
    public bool H { get; set; }
    public bool I { get; set; }
    public virtual Module Module { get; set; }

}
}

  • Not clear what your question is. What do you mean _once the user clicks on the module for create_? In which view, what element is clicked? –  Nov 20 '14 at 04:45
  • Its a link from the Details View to go to Create View @StephenMuecke – Faiz Everywhere Nov 20 '14 at 05:23
  • You don't have any links in your `Details` view. Do you mean you want to know how to render a link that redirects to the `Create` view? –  Nov 20 '14 at 05:26
  • Nope. I am trying to figure out how to change my dropdown list to just a label where the number is gotten from the ter @Model.Code. – Faiz Everywhere Nov 20 '14 at 05:39
  • Still not clear. Do you want a dropdown or not (why render it if you don't want one)? If you do, what do you want to render in it (your usage currently makes no sense) Are you trying to display a collection of `Module` items and display the value of `Module.Code`, but bind its `ID` property to `Learn.ModuleID`? –  Nov 20 '14 at 05:46
  • Yes i want to change the drop down list. I want to display the current module selected. – Faiz Everywhere Nov 20 '14 at 06:20
  • This is able to be done on my Details View by using @Model.Code but unable to be done on my Create View – Faiz Everywhere Nov 20 '14 at 06:20
  • Then you need to edit your question to include the models for both `Module` and `Learn` so I can give you an answer –  Nov 20 '14 at 06:22
  • Is there any other things needed to assist you in helping me @StephenMuecke ? – Faiz Everywhere Nov 20 '14 at 06:27

1 Answers1

0

In your GET method for create you need to generate a SelectList that represents the options you want to render in the dropdown. Ideally you should be using a view model to represent what you want to display/edit, but in this example, I'll just assign in to ViewBag

var modules = // call database to get all the modules
ViewBag.ModuleList = new SelectList(modules, "ModuleId", "Code");

View

@model Module1.Models.Learn
....
@using (Html.BeginForm()) {
  ....
  @Html.DropDownListFor(m => m.ModuleID, (SelectList)ViewBag.ModuleList, "-please select-")
  ....
}

or if your using a view model (which you should, refer What is ViewModel in MVC?) then it would be

@Html.DropDownListFor(m => m.ModuleID, Model.ModuleList, "-please select-")

Note the value of property Module.Code will be the display text, and the value of property Module.ModuleId will be bound to the Learn.ModuleID property. If you set the value of Learn.ModuleID in the controller before passing it to the view, then the text for that Module will be displayed, otherwise the "-please select-" option will be selected.

Note ALWAYS use strongly typed helpers - DropDownListFor() not DropDownList()

Edit

Based on further comments by OP, to display the value as 'readonly'. In the controller, get the Code value for the Module and assign to a view model or ViewBag property

var module = db.Modules.Where(m => m.ModuleId == learn.ModuleId).FirstOrDefault();
if (module != null)
{
  ViewBag.Code = module.Code;
}

and in the view

<div>@ViewBag.Code</div>

or

@Html.DisplayFor(m => m.Code) // if using a view model

and if you want to post back the value as well, include a hidden input for the ModuleID

@Html.HiddenFor(m => m.ModuleId)
Community
  • 1
  • 1
  • public ActionResult Create(int ID) { ViewBag.ModuleId = new SelectList(db.Modules, "ModuleId", "Code"); Learn model = new Learn(); model.ModuleId = ID; return View(model); } – Faiz Everywhere Nov 20 '14 at 06:59
  • No. `ViewBag.ModuleList = new SelectList(db.Modules, "ModuleId", "Code");` Your assigning the options `ViewBag.ModuleList` which is then used by `@Html.DropDownListFor()` to render the options in the ` –  Nov 20 '14 at 07:04
  • I received an error saying Module1.Models.Learn does not contain a definition for 'ModuleList' and no extension method 'ModuleList' accepting a first argument of type 'Module1.Models.Learn' could be found. A solution the project gave me was to create a ModuleList stub in my Learn Model. – Faiz Everywhere Nov 20 '14 at 07:10
  • Look at my answer. You not using a view model so it needs to be `@Html.DropDownListFor(m => m.ModuleID, (SelectList)ViewBag.ModuleList, "-please select-")` –  Nov 20 '14 at 07:15
  • Is it because i'm not using a view model? – Faiz Everywhere Nov 20 '14 at 07:15
  • Is there a way for me to change it from dropdown to Label or something that cannot be change by the user? – Faiz Everywhere Nov 21 '14 at 04:00
  • Sure. I'll update answer shortly (I was confused by you question) –  Nov 21 '14 at 04:08
  • sorry for this part `m => m.ModuleId == learn.ModuleId` the learn is gotten from where? – Faiz Everywhere Nov 21 '14 at 04:54
  • Looking at your last question, I assume you are doing something like `public ActionResult Create(int ID) { Learn model = new Learn(); model.ModuleID = ID; ...` so it could be `m => m.ModuleId == ID` or `m => m.ModuleId == model.ModuleID` –  Nov 21 '14 at 05:03
  • Okayy thank you again for your help. You have been great help to me. – Faiz Everywhere Nov 21 '14 at 05:05
  • Mr Stephen I received this error when creating. `A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = FK_dbo.Learns_dbo.Modules_ModuleId ]` I figured it was because now that the Label has become a label it cause this error. Is there a way to solve this? – Faiz Everywhere Nov 21 '14 at 07:22
  • Did you add the hidden input (last line of my answer)? –  Nov 21 '14 at 07:23
  • Where is that supposed to be added? Inside the same View? – Faiz Everywhere Nov 21 '14 at 07:28
  • I put @HTML.HiddenFor(m=>m.ModuleId) is that the problem? – Faiz Everywhere Nov 21 '14 at 07:30