user2889507
user2889507

Reputation: 1

from data to controller and passing it to form

I'm doing my first steps in mvc and I need help. I'm passing data from view to this controller and I need to pass the selected items with there details to a different view (that is a form that the user add his email details) and I cant figure out how to . This is how I'm getting the details to the controller from the submitted form

    public ActionResult list()
    {
        var AllItems = db.menu.ToList();
        Mapper.CreateMap<Menu, SelectableMenu>();

        return View(AllItems.Select(m => new SelectableMenu { price = m.price, MenuId = m.MenuId, Name = m.Name })
        .ToList());
    }


    [HttpPost]
    public ActionResult List(IEnumerable<SelectableMenu> item)
    {
        var userSelectedMenu = item.Where(m => m.IsSelected).Select(m => m.Name + m.price + m.MenuId);
        if (userSelectedMenu != null && userSelectedMenu.Any())
        {
            return View("bla");
        }

        return View();
    }

Upvotes: 0

Views: 61

Answers (3)

You will need twosteps for this

Step 1

Make a model(it is more effective) use it in a view to pass your data to controller through post in submission of form.

Step 2

Receive the data into the controller method then use
return View("yourNewpage","yourdatamodelobject"); in the controller action to pass the data in the action result view of another page.

Alternatively, if the view is in another controller

then you can receive data here in the post action method and use Return RedirectToAction("ActionName", "ControllerName", "DataModelObject") to pass to a diffrent controller

Upvotes: 0

Jatin patil
Jatin patil

Reputation: 4298

You can return different view using return View("ViewName",model)

For eg:

[HttpPost]
public ActionResult List(IEnumerable<SelectableMenu> item)
{
    var userSelectedMenu = item.Where(m => m.IsSelected).Select(m => m.Name + m.price + m.MenuId);
    if (userSelectedMenu != null && userSelectedMenu.Any())
    {
        return View("YourDiffrentViewName",userSelectedMenu); // This will pass your model to your Different view
    }

    return View();
}

Then in your new view you will have to strongly typed it with your model.

For eg :

Your view will be as follows:

@model ProjectName.models.YourClassName //Your class/model namespace

 @using(Html.BeginForm())
 {
       @Html.TextBoxFor(m => Model.Property) //This will create textbox for your property
<input type="submit" value="Submit" /> 
}

For more on stronly typed views visit:

  1. http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/strongly-typed-views-in-mvc/

  2. http://www.howmvcworks.net/OnViews/BuildingAStronglyTypedView

Upvotes: 0

Tomasz Wakulak
Tomasz Wakulak

Reputation: 139

Use method ReditectToActionstring actionName, string controllerName, Object routeValues)

for details go to: http://msdn.microsoft.com/en-us/library/dd460311(v=vs.108).aspx

Upvotes: 1

Related Questions