-1

How can I create a view model to calculate the value from banhotosaid + transpote Id, and than return the total for a view.

// GET: AgendaBTs/Create
        public ActionResult Create(int id, int pet)
        {

            ViewBag.BanhoTosaId = new SelectList(db.BanhoTosas, "BanhoTosaId", "Tipo");
            ViewBag.ClienteId = new SelectList(db.Clientes, "ClienteId", "Nome", id);
            ViewBag.PetId = new SelectList(db.Pets, "PetId", "PetNome", pet);
            ViewBag.TransporteId = new SelectList(db.Transportes, "TransporteId", "Tipo");

            if (ViewBag.BanhoTosaId.SelectedValue != null && ViewBag.TransporteId.SelectedValue != null)
            {
                BanhoTosa BT = db.BanhoTosas.Find(ViewBag.BanhoTosaId);
                Transporte TS = db.Transportes.Find(ViewBag.TransporteId);
                decimal valorSoma = BT.Valor + TS.Valor;
                ViewBag.Total = valorSoma;
            }
            else
            {
                ViewBag.Total = 0;
            }

            return View();

I hope you guys can help me.

Thank's

caio oliveira
  • 11
  • 1
  • 6

1 Answers1

-1

How are you populating your dropdownlist?

@Html.DropDownList("TransporteId", null, htmlAttributes: new { @class = "form-control" })

You are passing a null to the datasource. You should pass a valid datasource, e.g.

@Html.DropDownList("TransporteId", (IEnumerable<SelectListItem>)ViewBag.BanhoTosaId,null, new { @class = "form-control" })

It seems that you are passing a value to the action which makes it go to the else statement:

 else
 {
   ViewBag.Total = 0;
 }

That's why it was set to zero. So can you debug it and show what parameter is passed to the action.

Also replace your if condition with this and correct your code since the selections of the combos should be passed as parameters to your action method:

if (ViewBag.BanhoTosaId != null && ViewBag.SelectedValue != null)
{
  //you cannot pass the whole list to the Find Method, you should pass a valid integer or key 
  // BanhoTosa BT = db.BanhoTosas.Find(ViewBag.BanhoTosaId);
  BanhoTosa BT = db.BanhoTosas.Find(id);
  //Transporte TS = db.Transportes.Find(ViewBag.TransporteId);
   BanhoTosa BT = db.BanhoTosas.Find(pet);   
}
Hussein Salman
  • 7,806
  • 15
  • 60
  • 98
  • now the sistem throw An exception of type 'System.ArgumentOutOfRangeException' in this line BanhoTosa BT = db.BanhoTosas.Find(ViewBag.BanhoTosaId); – caio oliveira Mar 12 '18 at 17:46
  • Can you update the question's code to what you are trying and state at which line is the error happening? – Hussein Salman Mar 12 '18 at 17:47
  • Yes this is because the BanhoTosaId is a list and you are expexting an valid integer, i assume – Hussein Salman Mar 12 '18 at 17:47
  • No i'm trying to get the id e than in table there's a column which is a decimal the name of the column is Valor – caio oliveira Mar 12 '18 at 17:54
  • I add an Image from the DB – caio oliveira Mar 12 '18 at 17:57
  • What is the question? probably you should ask another question, you can't solve your problems in one single question – Hussein Salman Mar 12 '18 at 18:06
  • BUT BanhoTosa BT = db.BanhoTosas.Find(id); AND (pet) is from the viewbag.ClienteId and PetId – caio oliveira Mar 12 '18 at 18:11
  • Ok here is my Razor view, I realy appriciate your help. – caio oliveira Mar 12 '18 at 18:21
  • I can't use viewbag this way @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class . Cause i'm already passing the name of the view bag in the dropdownlist look at the image that i sended in the question – caio oliveira Mar 12 '18 at 19:13
  • Hey - do you expect people to answer and solve all your problems? You should do some work. Or just go and post new question for new problem you are facing – Hussein Salman Mar 12 '18 at 19:33
  • https://stackoverflow.com/questions/49243464/passing-a-viewbag-to-a-dropdownlist link to the new question =( – caio oliveira Mar 12 '18 at 19:55
  • This answer is completely wrong and has nothing to do with the issue –  Mar 12 '18 at 20:44
  • @StephenMuecke Really! Well the question was updated 100 times and the fixes was related to the question. – Hussein Salman Mar 12 '18 at 21:00
  • @StephenMuecke you can check the history and the comments before judging – Hussein Salman Mar 12 '18 at 21:01
  • @StephenMuecke can you help do that? PS: I edited the question. – caio oliveira Mar 12 '18 at 23:00
  • @caiooliveira. Its impossible to understand what you trying to do here, but for a start, stop using `ViewBag`. Create a view model containing all the properties you need in the view (will will contain `int SelectedBanhoTosa` and `IEnumerable BanhoTosaList` etc for use in your dropdownlists. And it will include a property `int Total` (or `double` or whatever). But you `if` block makes no sense, so I have no idea what your trying to do, and what you want the value of `Total` to be. –  Mar 12 '18 at 23:05