Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to set Selected option to a ViewBag so that when that item is selected specific code is run. It is running only the first item all the item. 


What I have tried:

Below is my code. Any help will be appreciated.


if (RecurringSeries.Weekly == weekly)
{
   code  goes here
}
else
if (RecurringSeries.Monthly == monthly)
{
   code  goes here
}
 
 
 if (RecurringSeries.Weekly == weekly)
  {
   ViewBag.weekly = weekly;
   }
  else
  {
  ViewBag.monthly = monthly;
  }			
			
<div class="form-group">
  <label>Recurring Series</label>
 <select id="ddRecurring" class="form-control">
  <option value="@ViewBag.weekly">Weekly</option>
 <option value="@ViewBag.monthly">Monthly</option>
</select>
</div>
Posted
Updated 16-Feb-19 19:28pm
Comments
Bryian Tan 17-Feb-19 0:31am    
and the problem is?

1 solution

I hope this will work for you as you are trying to keep selected value in a ViewBag.

public class UserVM
{
   public string Year { set; get; }
   public string Month { set; get; }
   public string Day{ set; get; }
}


Set value in a viewbag and bind

[HttpGet]
public ActionResult Register(User user)
{
     // It should be better to read this data from database
     ViewBag.Year = new SelectList(
                                    new List<SelectListItem>
                                    {
                                        new SelectListItem { Text = "1368", Value = "1987" },
                                        new SelectListItem { Text = "1369", Value = "1988"}, //....
                                    }, "Value" , "Text");
    // Also other properties like Month and Day
     return View();
}


@using (Html.BeginForm())
{
    <div class="form-group">
        @Html.DropDownListFor(m => m.Year, (SelectList)ViewBag.Year, "Select one")
    </div>

    <button type="Submit" class="btn btn-success submit">Send</button>
}


Get the selected value and set it into another view bag.

[HttpPost]
public ActionResult Register(VM model) 
{
    if(!ModelState.IsValid) 
    {
       ViewBag.SelectedYear = model.Year;
    }
    return View();
}
 
Share this answer
 
Comments
Member 1283347 17-Feb-19 1:54am    
I am not using ViewModel and they are not SelectListItem. I have html Option selected dropdown for users to select weekly or monthly Recurring events. When a weekly item is selected from the drop that an if statement that execute weekly events should be executed. It is not an action method.
Arkadeep De 19-Feb-19 15:11pm    
What is your problem then??
Member 1283347 19-Feb-19 18:35pm    
I have resolved my issue. I was using enum RecurringSeries.Weekly which wasn't properly callled. Changed to database table column and everything works fine. Thanks for your help.
Arkadeep De 28-Feb-19 12:38pm    
(y)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900