NeedAnswers
NeedAnswers

Reputation: 1435

Insert new rows into existing table on view

This is my table in the view (cshtml) :

  <table id="tblketquatk" class="thongke2socuoi">
        <tbody>
            <tr>
                <th>Số</th>
                <th>Ngày về gần nhất</th>
                <th>Số lần xuất hiện</th>
                <th>Số ngày chưa về</th>
            </tr>
        </tbody>
    </table>

In the controller :

    [HttpPost]//Run action method on form submission
    public ActionResult LastTwoSubmit(string cityID, string numbers, int days, bool onlySpecial)
    {
        // get the result from sql server based on the parameters 
        // now i want to append the result to the table tblketquatk
        return View();
    }

Now I want to append the result to the table tblketquatk, is there anyway to do this without using Javascript?

I've done this before using JQuery, which will use Ajax to append the result into the exisiting table without reloading the page.

Link to JsFiddle for a better look.

What I want is how to insert the newly returned dataset into the table, and the parameters on the form remain unchanged/reset.

Any help is greatly appreciated!

Upvotes: 0

Views: 957

Answers (1)

user3559349
user3559349

Reputation:

You need models to bind to so that model can be returned to the view

public class SearchViewModel
{
  public int Days { get; set; }
  ....
}

public class MainViewModel
{
  public SearchViewModel Search { get; set; }
  // Add a property for the collection of items you are rendering in the table
}

View

@model MainViewModel
@using (Html.BeginForm())
{
  @Html.TextBoxFor(m => m.Search.Days)
  ....
  <input type="submit" ... />
}
// add loop to create table rows

Controller

[HttpPost]
public ActionResult LastTwoSubmit(MainViewModel model)
{
  // use the values of model.Search to query the database and add to the model collection
  return View(model);
}

because you form properties are now bound to a model and your returning that model, the values will be retained when you return the view.

Upvotes: 1

Related Questions