Flip the model instance through controller return to view

Related searches
  • I created a model, I created instances of the model in the controller

  • I want to send this instance data to partial view (does not work)

Model

 public partial class userDaty_Model
    {
        public int liczbaDniM { get; set; }

        public int numerMiesiacaM { get; set; }

        public int numerRokuM { get; set; }
    }



// which is part of ParentView


public partial class ParentView
    {
        public userDaty_Model Model4 { get; set; }
    }

Ajax code in main view from which I am calling partialView.

 var userDate = {
             numerMiesiaca: $("#id_sb_month").dxSelectBox("instance").option("value"),
             numerRoku: $("#id_sb_year").dxSelectBox("instance").option("value"),
             liczbaDni: liczbaDni
         };


        $.ajax({

            url: "@Url.Action("PartialTabelaEcp", "Home")",
            type: "POST",
            dataType: "json",
            data: {"userDate": JSON.stringify(userDate)},
            cache: false,
            success: function (data) {
                $("#kartaEcp").html(data);
            },
            failure: function (error) {
                alert(error);

            },
            error: function (error) {
                alert(error);
            }
        });

Controller:

 [HttpPost]
        public ActionResult PartialTabelaEcp(string userDate)
        {
int liczbaDni =2;
int numerMiesiaca = 6;
int numerRoku = 12;

userDaty_Model userDaty = new userDaty_Model();
            userDaty.liczbaDniM = liczbaDni;
            userDaty.numerRokuM = numerMiesiaca;
            userDaty.numerMiesiacaM = numerRoku;


// return, which returns the view
 return PartialView(@"~/Views/Home/_TabelaEwidencja.cshtml");

// return who I want to return but gives me nothing
 return PartialView(@"~/Views/Home/_TabelaEwidencja.cshtml", userDaty );
}


***edit
 I changed and I have "return PartialView("_TabelaEwidencja", userDaty);"and it is still the same as it was

I set the red dot to return, when there is no instance of the 'userDate' model it goes through return

When it's in Error

jquery.js:9837 POST https://localhost:44362/Home/PartialTabelaEcp 500 (Internal Server Error)

after : Error

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

partialView

@model ParentView


// ...

does anyone know what i am doing wrong?

Change like below:

1.Ajax(change from dataType:"json" to dataType:"html"):

$.ajax({
    url: "@Url.Action("PartialTabelaEcp", "Home")",
    type: "POST",
    dataType: "html",   
    data: {"userDate": JSON.stringify(userDate)},
    cache: false,
    success: function (data) {
        $("#kartaEcp").html(data);
    }
});

2.Controller:

[HttpPost]
public ActionResult PartialTabelaEcp(string userDate)
{
    int liczbaDni = 2;
    int numerMiesiaca = 6;
    int numerRoku = 12;

    userDaty_Model userDaty = new userDaty_Model();
    userDaty.liczbaDniM = liczbaDni;
    userDaty.numerRokuM = numerMiesiaca;
    userDaty.numerMiesiacaM = numerRoku;

    var data = new ParentView()
    {
        Model4 = userDaty
    };
    return PartialView("~/Views/Home/_TabelaEwidencja.cshtml", data);
}

ASP.NET MVC - Passing Data From Controller To View, you on ASP.NET MVC - passing the data from Controller to View. Thus, let's start and flip to your Visual Studio 2015. ASP.NET Erase the code of ViewData and pass the object of model class in return view. ASP.NET. This @ModelType directive allows you to access the movie that the controller passed to the view by using a Model object that's strongly typed. For example, in the Details.vbhtml template, the code passes each movie field to the DisplayNameFor and DisplayFor HTML Helpers with the strongly typed Model object.

You should use the right model in your @model if you give him the Daty model but say him it is a Parent View Model it throws an error

Multiple Views and DisplayMode Providers in ASP.NET MVC 4 , NET MVC 4 provide a neat way of separating page content from the way it is in the view, then you pass this data down to the view object by return View(model ); You use the Instance static member to access the singleton instance of the DisplayModeProvider class and flip through the Modes property. This @model directive allows you to access the movie that the controller passed to the view by using a Model object that's strongly typed. For example, in the Details.cshtml template, the code passes each movie field to the DisplayNameFor and DisplayFor HTML Helpers with the strongly typed Model object.

That is because you can't return full paths to views, views are always relative to a folder inside the Views folder and the name of the controller (this can be changed, of course), like, for example:

/Views/Home
/Views/Admin

So, you should be calling instead:

return PartialView("_TabelaEwidencja", userDaty);

Also, in your view, declare the right type of model: either userDaty_Model or dynamic, this is using the @model keyword.

ASP.NET MVC Postbacks and HtmlHelper Controls ignoring Model , In effect this means that MVC Views can't update the value of controls during a POST back using the model.user = user; return View(model);. no - You need to have one controller which will return something like : return View(model.Stage == 0); With the view I gave above, Which will then uses the true/false to determine which controller / action to call next. Thanks. Carl

Accessing Your Model's Data from a Controller, A request to the Movies controller returns all the entries in the Movies the controller passed to the view by using a Model object that's strongly� I'm going to discuss how Controllers interact with Views and specifically cover ways you can pass data from a Controller to a View to render a response back to a client. So, let's get started. ViewBag ViewBag is a very well known way to pass the data from Controller to View & even View to View. ViewBag uses the dynamic feature that was added in

Understanding the Model-View-Controller (MVC) Architecture in Rails, The controller retrieves the data it needs from the model in order to Flip back to the section on object-oriented programming (OOP) We can confirm that our Story object hasn't been saved by checking the return value of� Your passing an anonymous object!. Create a view model with those properties and project your query into the view model. And pass the model to the view (do not use ViewBag). And show your code, not images of it – user3559349 Aug 27 '16 at 12:08

mvc view results: return View() - will return a view named the same as the action method from the corresponding path of the controller HomeContoller and Action Index will look in ~/Views/Home/ and ~/Views/Shared/ for Index.cshtml or Index.vbhtml

Comments
  • a 500 error indicates that an error was thrown on the server side (in your PartialTabelaEcp controller action). surround the code inside the controller action with a try/catch block and set a breakpoint in the catch section. Run your code, find what the exception is then fix it yourself or report back here and we'll be of more help to you. also it doesnt look liek the model you return in your action matches the expected model in your partial view and you shouldnt need to specify the entire path to the partial view
  • @GregH I update, I set the red dot to return, when there is no instance of the 'userDate' model it goes through return, When it's in Error jquery.js:9837 POST localhost:44362/Home/PartialTabelaEcp 500 (Internal Server Error)
  • then it sounds like your error is during the compilation of the razor view which would indicate your passing teh wrong model to your partial view or have some other issue with you partial view. you should be able to override OnException in your controller (there are tons of examples on how to do this) and set a breakpoint in there to find the actual error. you gave me the error message from the client side which isn't what you need to find. you need to find where it's failing server side
  • I changed and I have "return PartialView("_TabelaEwidencja", userDaty);"and it is still the same as it was