zerd
zerd

Reputation: 27

ASP.NET MVC 4 - Partial views and Ajax.Actionlink

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?

Upvotes: 2

Views: 3473

Answers (1)

Chris
Chris

Reputation: 1601

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

Upvotes: 4

Related Questions