0 Replies - 560 Views - Last Post: 07 February 2017 - 03:30 AM

#1 [email protected]  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-January 17

Calculate two value in mvc using dropdownlist

Posted 07 February 2017 - 03:30 AM

Hi,

I have one problem occur when enter value in textbox and try to calculate the two numbers,
and than i select one value in dropdown list for any operation but i can't get any result.


Index.cshtml
@{
    ViewBag.Title = "Operation";
}

<h2>Index</h2>

<div>
    @using (Html.BeginForm())
    { 
    <table>
        <tr>
            <td>first Number :- </td>
            <td>@Html.TextBox("firstNumber")</td>
        </tr>
        <tr>
            <td>Second Number :- </td>
            <td>@Html.TextBox("secondnumber")</td>
        </tr>
        <tr>
            <td>Operation :- </td>
            <td>@Html.DropDownList("Operation", (List<SelectListItem>)ViewBag.Ope,"select option")</td>
        </tr>
       
        
    </table>
    <input type="submit" value="Get Result" />
    }
    <p>
        Result :-
        @ViewBag.Ope;
    </p>
   
</div>





OperationController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EmployeeDetails.Controllers
{
    public class OperationController : Controller
    {
       
        // GET: Operation
       
        public ActionResult Index()
        {
            List<SelectListItem> Oper = new List<SelectListItem>();
            {
                Oper.Add(new SelectListItem { Text = "Addition", Value = "Add"});
                Oper.Add(new SelectListItem { Text = "Substract", Value = "Sub" });
                Oper.Add(new SelectListItem { Text = "Multiplication", Value = "Mul" });
                Oper.Add(new SelectListItem { Text = "Division", Value = "Div" });
 
            }
            ViewBag.Ope = Oper;
            return View();
        }

        [HttpPost]
        public ActionResult Index(double firstnumber,double secondnumber, string Operation)
        {
            double result = 0;
            switch (Operation)
            {
                
                case "Add" :
                      result = firstnumber + secondnumber;
                     break;
                case "Sub":
                    result = firstnumber - secondnumber;
                    break;
                case "Mul":
                    result = firstnumber * secondnumber;
                    break;
                case "Div":
                    result = firstnumber / secondnumber;
                    break;
            
            }
            ViewBag.Ope = result;
            return View();
        }
    }
}



CreateEmployee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EmployeeDetails.Models
{
    public class CreateEmployee
    {
        public int EmpId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public DateTime DOB { get; set; }
    }
}


Thanks.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1