-2

currently i am using ViewData and Viewbag for storing data and tempdata for redirection all are working fine.

i just want to know is there any optimize way to store data or controls values during postback in MVC ?

this is my controller method , i am passing two model classes from controller to view using dynamic object.

    [HttpGet]
    public ActionResult Index(int? page)
    {

        IList<Customer> _CustomerList = new List<Customer>();
        IList<LCOddl> LCODDL= new List<LCOddl>();

        // some logic to bind both classed with database ..

        dynamic DashboardClassed = new ExpandoObject();
        DashboardClassed.LCODDL = LCODDL;
        DashboardClassed.CUSTOMER = _CustomerList.ToPagedList(page ?? 1, 30);

   return View(DashboardClassed);          

    }

here is my razor view which use dynamic object :

@using PagedList;
@using PagedList.Mvc;
@model dynamic
@{
    Layout = null;
 }
 @using (Html.BeginForm("SearchMethod", "Dashboard", FormMethod.Post))
                            {
 @foreach (Models.LCOddl item in Model.LCODDL)
     {
        // Render HTML
     }

 @foreach (Models.Customer item in Model._CustomerList )
     {
        // render html 
     }
<div class="form-group"><button type="submit" class="btn btn-success">Go</button></div>

   }

after button click from my razor view SearchMethod from Dashboard Controller will called .

Now here i want two things :

  1. Don't want to bind customer class again with database - for that i am using tempdata .
  2. i have to maintain paging using PagedList.

again my question is same should i use tempdata for storing data or is there any other optimize way to do the same.

Vinod Saini
  • 137
  • 2
  • 9
  • 3
    Try binding it to a `model`.. – Guruprasad J Rao May 03 '16 at 04:20
  • you mean fetch the data from database using model classses ? – Vinod Saini May 03 '16 at 04:26
  • Fetch it from database for once or accept it from client through forms. It will be there on `postback`. That's how `model` works. without knowing what you are trying to achieve or how you code looks, we cannot give exact suggestions here.. – Guruprasad J Rao May 03 '16 at 04:28
  • @vinodsaini You are going to need to submit some code. It is a valid question but you need to show something before it will get answered. Asking for suggestions is not going to get you an answer. This question has been asked many times before on SO. I provided a simple answer. – Brendan Vogt May 03 '16 at 07:36

1 Answers1

1

The best way to retain your form values during a post back is to bind your view to a view model.

I wrote an answer as to what a view model is, please go and read it:

What is ViewModel in MVC?

Let us go through it step by step. I do not know what you are trying to achieve so I am going to use my own example. Let us assume you are trying to add a new user.

First of all create your view model for your form data. Your view model is different to your domain model in that it only contains the data that you want to use on your view. Sometimes your domain model might contain lots of data, but you do not want to add or edit everything. So you create a view model with only the data that you want to add or edit. In our example, I only want to add a first and last name for my user, so I will create 2 properties, namely FirstName and LastName:

public class UserViewModel
{
     public string FirstName { get; set; }

     public string LastName { get; set; }
}

The above view model will be instantiated and be passed to the view from your action method. When your form is posted your action method will receive this view model as a parameter. It will contain the values of first and last name (if entered):

public class UserController : Controller
{
     private IUserRepository userRepository;

     public UserController(IUserRepository userRepository)
     {
          this.userRepository = userRepository;
     }

     public ActionResult Create()
     {
          UserViewModel model = new UserViewModel();

          return View(model);
     }

     [HttpPost]
     public ActionResult Create(UserViewModel model)
     {
          // Do some form validation
          if (!ModelState.IsValid)
          {
               return View(model);
          }

          // If form validation succeeds do what you need to do
     }
}

In your create view it will receive this view model. All that you then need to do is to create a form with 2 textboxes for first and last name and a submit button:

@model YourProjectName.ViewModels.UserViewModel

@using (Html.BeginForm())
{
     <div>
          @Html.TextBoxFor(m => m.FirstName, new { maxlength = "50" })
          @Html.ValidationMessageFor(m => m.FirstName)
     </div>
     <div>
          @Html.TextBoxFor(m => m.LastName, new { maxlength = "50" })
          @Html.ValidationMessageFor(m => m.LastName)
     </div>
     <div>
          <button type="submit">Save</button>
     </div>
}

This is really a basic example. Go through it, play around with the code so that you can implement it into your scenario.

Community
  • 1
  • 1
Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234