2

I am trying to put on my homepage some link that render partial views - I want to display some info from the database when users click a link: the link should be replaced by text on the same page. I followed a tutorial but I cannot get it to work in my project. Here is what I have:

My Home/Index view:

<div id="NeZaman">
@Ajax.ActionLink("Ne Zaman Gelcekmiş?", "NeZaman", new AjaxOptions {
                                        UpdateTargetId="NeZaman",
                                        InsertionMode = InsertionMode.Replace,
                                        HttpMethod="GET" })
</div>

My HomeController:

    private CaglaContext db = new CaglaContext();

    public PartialViewResult NeZaman()
    {
        var neZaman = db.Caglas.Where(c => c.Id == 1).Select(c => c.NeZamanGelcek).FirstOrDefault();
        return PartialView("_NeZaman", neZaman);
    }

My partial view (_NeZaman.cshtml):

    @model caglageldimi.Models.Cagla

<p>
   @Model.NeZamanGelcek
</p>

My Model(Cagla.cs):

public class Cagla
{
    public int Id { get; set; }
    public bool GeldiMi { get; set; }
    public string NeZamanGelcek { get; set; }
    public string Nerdeymis { get; set; }
}

So I'm passing in a neZaman value that the partial view is supposed to use, but how?

1
  • Atm I'm getting this error: The model item passed into the dictionary is of type 'System.String', but this dictionary requires a model item of type 'caglageldimi.Models.Cagla'. No idea how to fix that. Commented Jul 31, 2012 at 14:04

1 Answer 1

4

You've set your patial view's model to your class:

caglageldimi.Models.Cagla

But you're passing a string:

db.Caglas.Where(c => c.Id == 1).Select(c => c.NeZamanGelcek).FirstOrDefault();

Your select statement is only grabbing the "NeZamanGelcek" string property value to send to your partial view.

Changing your partial view's model to System.String should fix it:

@model System.String

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.