1 Replies - 4872 Views - Last Post: 09 January 2011 - 01:34 AM

#1 zauii   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 02-January 11

Annoying error

Posted 08 January 2011 - 12:41 PM

Hello,

The application prototype is about just handling books and authors and being able to choose an author for a book via a dropdownlist and also edit/delete.

I've done some work since my last post with the dropdownlists but right now i get an error stating the following when i try to Edit/Create a book:
The ViewData item that has the key 'Bok.FId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
Line 15: <%: Html.DropDownListFor( m => m.Bok.FId , Model.Authors)%>

Ive had it working before , not sure where ive gone wrong now with this annoying error.

This is how my controller methods look, the view..
Create

    // Create Partial Vyn Bok
        public ActionResult CreateBok()
        {
            var model = this._repository.GetForfattare().ToList();
            SelectList selList = new SelectList(model, "FId", "Fnamn"); 

            var BokModel = new BokViewModel();
            BokModel.Authors = selList; 

            //ViewData.Add("Authors", selList);
            return View("CreateBok", BokModel);
        }

        // Post CreateBok
        [HttpPost]
        public ActionResult CreateBok(BokViewModel model)
        {
            if (TryUpdateModel(model))
            { 
                this._repository.Add(model.Bok);
                this._repository.Save();

                return View("SavedBok", model);
            }
            return View("CreateBok", model);
        }




Edit:
        // Edit Bok
        public ActionResult EditBok(int id)
        {
             var authors = this._repository.GetForfattare().ToList();       
             var bokToEdit = this._repository.GetBokById(id);  
             var BokModel = new BokViewModel();
  
             BokModel.Bok = bokToEdit;            
             SelectList selList = new SelectList(authors, "FId", "Fnamn", bokToEdit.FId);
             BokModel.Authors = selList;
              
             return View (BokModel);
        }

        // Post Edit Bok
      [HttpPost]  
      public ActionResult EditBok( int id, BokViewModel model )  
      {  
          if ( TryUpdateModel( model ) )  
          {  
              model.Bok.BokId = id;  
              this._repository.Update( model.Bok );  
              this._repository.Save( );  
      
             return View( "SavedBok", model );  
         }  
         return View( "EditBok", model );  
      }  



View:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<slutprojektet.Models.BokViewModel>" %>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm())
   { %>
<%: Html.ValidationSummary(false, "Fel inträffade. Korrigera felen och försök igen.") %>
<% Html.ValidateFor(model => model.Bok.Sidor); %>
<% Html.ValidateFor(model => model.Bok.Publicerad); %>
<% Html.ValidateFor(model => model.Bok.Titel); %>

<div class="discription">
    <%: Html.LabelFor(model => model.Authors)  %>:
</div>

<div>
    <%: Html.DropDownListFor( m => m.Bok.FId , Model.Authors)%>  
</div>


<div class="discription">
    <%: Html.LabelFor(model => model.Bok.Sidor)  %>:
</div>
<div>
    <%: Html.TextBoxFor(model => model.Bok.Sidor, new { @class = "tb", maxlength = 5 })%>
</div>

<div class="discription">
    <%: Html.LabelFor(model => model.Bok.Publicerad)%>:
</div>
<div>
    <%: Html.TextBoxFor(model => model.Bok.Publicerad, new { @class = "tb", maxlength = 10 })%>
</div>

<div class="discription">
    <%: Html.LabelFor(model => model.Bok.Titel)%>:
</div>
<div>
    <%: Html.TextBoxFor(model => model.Bok.Titel, new { @class = "tb", maxlength = 25 })%>
</div>


<div>
    <input class="substyle" type="submit" value="Spara" />
</div>
<% } %>
<div class="backlink">
    <%: Html.ActionLink("Tillbaka", "IndexBok") %>
</div>



This post has been edited by zauii: 08 January 2011 - 12:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Annoying error

#2 Core   User is offline

  • using System.Linq;
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,101
  • Joined: 08-December 08

Re: Annoying error

Posted 09 January 2011 - 01:34 AM

Could you please post the implementation of BokViewModel? You are basically linking Bok.FId to Authors - two unrelated components from what I can see, unless there is a custom property implementation. And that's exactly why we need to see BokViewModel.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1