0

I have referred few question (Q1, Q2) regarding this question but I think mine is little different.

I have created a login page and on successful login it should open Index view of home controller. As my index view requires a model to bind table data I need to pass the model object along with view in login button action. I hope you get the scenario.

Here's my LoginController:

public ActionResult Login()   // Get method for Login
{
    return View("Login");
}

[HttpPost]  // Post method on Login
public ActionResult Defaults(string username, string password)
{
    FloorFactor ff = new FloorFactor(); // tried to create model object to pass in view

    try
    {
        Login_Details login_detail = db.Login_Details.Single(x => (x.UserName == username) && (x.Password == password));

        if (login_detail != null)
        {
            return View("../Home/Index",ff ); // need to get view Index  of home controller.
        }
    }
    catch {  }

    return View("Login");
}

Required part of Index.cshtml:

@model IEnumerable<Pricing_Tool_Test.FloorFactor>

@{
    ViewBag.Title = "Index";
}

Error:

enter image description here

I know model required IEnumerable kind of model object but in my defaults method it doesn't allow me to pass IEnumerable object of model.

Can anyone help? How can I achieve this?

Edit: both controllers are different. Login button needs to load view of HomeController. It generates exception shown in the screenshot, if I comment that line than it generates error mentioned above:

enter image description here

EDIT 2: Home/Create Coding

public PartialViewResult Create()
{
    return PartialView("_CreatePartial");
}

// POST: Home/Create
[HttpPost]
public ActionResult Create([Bind(Include = "FloorFactorPercentage,FromDate,ToDate")]FloorFactor floorfactor)
{
    if (ModelState.IsValid)
    {
        db.FloorFactors.Add(floorfactor);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
        
    return View(floorfactor);
}

Inner Exception Image

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Harshil Shah
  • 203
  • 1
  • 16
  • in your view just use @model Pricing_Tool_Test.FloorFactor as you are passing single element so don't need IEnumerable – Numan Ali May 24 '16 at 10:44
  • on successful login it loads Table of content on Index.cshtml so it is not single element so it requires IEnumerable – Harshil Shah May 24 '16 at 10:47
  • your code is just passing a single empty object called 'ff' – Numan Ali May 24 '16 at 10:49
  • Possible duplicate of [The model item is of type CookMeIndexViewModel, but requires a model item of type IEnumerable](http://stackoverflow.com/questions/4813307/the-model-item-is-of-type-cookmeindexviewmodel-but-requires-a-model-item-of-typ) – CodeCaster May 24 '16 at 11:01
  • @CodeCaster See the Edit. n i think no need to downvote post as the question you mentioned was not found in my search results. The heading of Linked question is a lot different. Downvote is used when you don't see ANY Search efforts or unclear – Harshil Shah May 24 '16 at 11:47
  • Show us the code of you Home/Create action and more on the inner exception you are getting. – Yogi May 24 '16 at 11:59

3 Answers3

2

You already understands the problem that the View is expecting IEnumerable<Pricing_Tool_Test.FloorFactor> and you are passing a single object of FloorFactor.

You could populate your model accordingly and pass it to view like -

List<FloorFactor> model = new List<FloorFactor>();
model.Add(ff);

and pass it to your View -

return View("../Home/Index",new List<FloorFactor> { model} );

Having said that, to me it make little sense to pass empty model to such controller. If you do not need collection of FloorFactor in your view, bind it with just the type, instead of IEnumerable.

@model Pricing_Tool_Test.FloorFactor

Edit - Following your edits, I suggest you to reconsider your design approach.

On a high level, if the login is successful, redirect to the Get action of you controller and NOT the POST. You should not need to pass IEnumerable<FloorFactor> instance to the view. Now you view should have controls binded with FloorFactor properties within an Form. And when the user fill these details, then call the Create action using POST on form submit, and save the changes in database.

Yogi
  • 9,174
  • 2
  • 46
  • 61
  • Too quick at typing, beat me to virtually the same answer. :) – hutchonoid May 24 '16 at 11:01
  • @hutchonoid - Quiet a coincidence. The fact is instead that I am a bit slow to post. :) – Yogi May 24 '16 at 11:10
  • Thanks for useful answer. It helped me a little. by using your code it still gives me erro but if i write `List model = new List(); return View("link",model) ` If i add these two lines it provides me Index view with Empty result. As i am passing empty model object. So, any idea how can i pass actual model with existing data ? – Harshil Shah May 24 '16 at 11:13
  • @HarshilShah - This is because you are passing empty object to the list - `FloorFactor ff = new FloorFactor();`. ff object is just initialized, it do not have any values in it. Just initialize the properties of object with proper values (if there are many ff like objects, do the same with all of them) and add to the list. Ideally you should be getting the populated model from your business layer. – Yogi May 24 '16 at 11:21
0

Just try your class name "FloorFactor" without using IEnumerable.because you are not sending a list. I Think this will solve your issue.

@model Pricing_Tool_Test.FloorFactor
@{
    ViewBag.Title = "Index";
}
Nifal Nizar
  • 895
  • 1
  • 8
  • 17
0

Either you have to return IEnumerable object.

Or in view, you need to just put it without IEnumerable.

@model Pricing_Tool_Test.FloorFactor
@{
    ViewBag.Title = "Index";
}