简体   繁体   中英

Model Data not passing to View in Asp Mvc

I have an action method which returns a partial view

public PartialViewResult GemByMonth(int id,string btn)
{
    if (btn == "bymnthbtn1")
    {
     var birthgembymonth1 = dbcontext.GemStoneByMonths.Where(p => p.GemStoneByMonthId == id).Select(q => new { q.GemEng, q.GemImage });
        return PartialView("_BirthGemByMonthEng", birthgembymonth1);
    }

    return PartialView();
}

My Partial View is

@model Astrology.Models.GemStoneByMonth

<div class="col-md-12">
    <div class="col-md-6" style="height: 100%;margin-top: 12%;font-size: 2em"> @Model.GemEng</div>
    <div class="col-md-6"><img style="height: 7em; margin-left: 7em; margin-top: 3em" src="data:image;base64,@System.Convert.ToBase64String(Model.GemImage)" /></div>
</div>

My model class

  public class GemStoneByMonth
    {

        public int GemStoneByMonthId { get; set; }
        public string EnglishZodiac { get; set; }
        public string MalayalamZodiac { get; set; }
        public string SanskritZodiac { get; set; }
        public string GemEng { get; set; }
        public string GemMal { get; set; }
        public byte[] GemImage { get; set; }
    }

I have 2 buttons. so to distinguish from which button the click event occurs i added values to my button which is accessed via string 'btn' variable.

But the model data i passed to the partial view is not passing to my partial view. can someone help me with it?

Linq query is lazy loading by default, until you explicitly call ToList() , FirstOrDefault() , First() or so on.

I believe you want to use FirstOrDefault() , if you want to get the single value. In addition, you need to pass the model as GemStoneByMonth instead of anonymous type.

public PartialViewResult GemByMonth(int id, string btn)
{
    if (btn == "bymnthbtn1")
    {
        var entity = dbcontext.GemStoneByMonths
            .FirstOrDefault(p => p.GemStoneByMonthId == id);

        var birthgembymonth1 = new GemStoneByMonth
        {
            GemEng = entity.GemEng,
            GemImage = entity.GemImage
        };    
        return PartialView("_BirthGemByMonthEng", birthgembymonth1);
    }
    return PartialView();
}

Use viewdata then as you are selecting some values from the whole model. You can't bind whole model where you are only selecting few properties.

var birthgembymonth1 = dbcontext.GemStoneByMonths.FirstOrDefault(p => p.GemStoneByMonthId == id);
ViewBag.GemEng = birthgembymonth1.GemEng;
ViewBag.GemImage = birthgembymonth1.GemImage;

Then in razor view get those values using:

<div class="col-md-12">
    <div class="col-md-6" style="height: 100%;margin-top: 12%;font-size: 2em"> @ViewBag.GemEng</div>
    <div class="col-md-6"><img style="height: 7em; margin-left: 7em; margin-top: 3em" src="data:image;base64,@System.Convert.ToBase64String(ViewBag.GemImage)" /></div>
</div>

When you use select then c# creates new dynamic object so binding GemStoneByMonth in the model won't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM