vidhi patel vidhi patel - 21 days ago 4
ASP.NET (C#) Question

Model returns null when passing the grid with calculation value in controller to the view

Here i have two filed on is allocation and other filed is amount. I want show third filed in ((Allocation/100)*Amount) n show in the third filed.

I have code For this but it is not worked.in View my Model return Null Value.here below i show the code of my Controller.

public ActionResult recommendedportfolio()
{
using (var db = new WFTradeEntities1())
{
return View(db.Schema_Details.ToList());
}
}
[HttpPost]
public ActionResult recommendedportfolio(int sName)
{
using (var db = new WFTradeEntities1())
{
var tr = db.Schema_Details.ToList().Where(C => C.RID == sName).Sum((x => x.Allocation / 100));
var total = db.Schema_Details.Sum((x => x.Lum_Sum_Amount * tr )&& (x =>x.SIPAmount * tr ));
return PartialView("_PortfolioTable", total);
}
}


so Please help me out i want two display that amount in third raw n in forth raw same as counting..
in this below image i can display my query with output.i have to fix lum_Sum_Amount 25000.

In Allocation some % are given 50% 20% 30% = total 100%.
what i do in view lum_Sum_Amount calculate with Allocation percentage value n getting some value that value display in total.

enter image description here

Answer

First of All you need to create a model

public class LINQDATA2
{
    public Int64 SID { get; set; }
    public Int64 RID { get; set; }
    public string Schema_Name { get; set; }
    public string SType { get; set; }
    public string InvestmentType { get; set; }
    public int Allocation { get; set; }
    public int Lum_Sum_Amount { get; set; }
    public float Total { get; set; }
}

Because dynamic column is not possible in LINQ.

Now perform query like

var x = data.Select(i => new LINQDATA2 { SID = i.SID, Total = (i.Allocation / 100) * i.Lum_Sum_Amount }).ToList();

and pass this model to partial view

Comments