Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Normal Asp.net We Write Code in !ispostback and write code in it server side...for example..

C#
private void Page_Load(object sender, System.EventArgs e)
{
    if(!IsPostBack)
    {
        lstCity.SelectedIndex = 1;
        whetherIsGood = true;

    }


}

how to Establish !ispostback concept in mvc4 razor. and where.....??????
reply..thanks in advance..
Posted
Comments
Raul Iloc 30-Mar-14 9:36am    
Did you try like I suggested in my solution?

1 solution

The management of user actions MVC is different then in ASP.NET.

Lets explain by using an example, supposing that from the user want to activate the Édit page by using a link or a button from Index page.

1.In response to the user action there must be a method in controller (that will be invoked from the client by using GET) named ViewResult Edit() that are used to create the Model and to return the ViewResult that will render the view and send it to the client browser. So this Edit() method is invoked only for the first GET (similar with IsPostback equal with false)!

2.Then the browser will show the Edit view (page) and the user will can use some controls that collect the user inputs and finally press Save submit button, that first will activate the client side validation and then, if the client side validation will succeed, it will generate a postback to the server.

3.On you controller there should exist 2nd method with the same name ActionResult Edit(EditModel model) but they should have some parameter (usually the model object used) and this method must have [HttpPost] attribute specified, and this method will be invoked from the client in 2nd step above. This method normally should made some logical validation of the entire user inputs, then try to save the input data into the database, also should manage the possible errors (so different actions results could be generated from its code).

You can see example in my next article: MVC Basic Site: Step 1 – Multilingual Site Skeleton[^]
 
Share this answer
 
v2
Comments
reshmatces 1-Apr-14 8:07am    
on my Gridview page Changing the data get Cleared automatically what to apply for that....
Raul Iloc 1-Apr-14 8:38am    
You should give me more details about: what grid are you using? when the data are cleared, at postback?
reshmatces 2-Apr-14 0:59am    
i am using Normal GridView ...eg

{
var grdLocation = new WebGrid(source: Model, rowsPerPage: 3, selectionFieldName: "selectedRow", canSort: false);
}
grdLocation.Pager(WebGridPagerModes.All);

<div class="Divgridmst">
@if (@Model != null)
{
@grdLocation.GetHtml(

tableStyle: "webgrid",
headerStyle: "webgridheader",
alternatingRowStyle: "webgridaltrowstyle",
selectedRowStyle: "select",
rowStyle: "webgridrowstyle",
footerStyle: "webgridfooter",

firstText: "First",
lastText: "Last",
nextText: "Next",
mode: WebGridPagerModes.All,
previousText: "Previous",
columns: grdLocation.Columns(
grdLocation.Column("location_code", "Location Code", style: "webgridwrap"),
grdLocation.Column("location_name", "Location", style: "webgridwrap"),
grdLocation.Column("country_name", "Country", style: "webgridwrap"),
grdLocation.Column("state_name", "State", style: "webgridwrap"),
grdLocation.Column("zone", "Zone", style: "webgridwrap"),
grdLocation.Column("city_name", "City", style: "webgridwrap"),
grdLocation.Column("location_address", "Address", style: "webgridwrap"),
grdLocation.Column("location_pin_code", "Pin Code", style: "webgridwrap"),
grdLocation.Column("location_telephone_no", "Telephone", style: "webgridwrap"),
grdLocation.Column("location_fax_no", "Fax No", style: "webgridwrap"),
grdLocation.Column("location_email_id", "Email ID", style: "webgridwrap"),
grdLocation.Column("report_name", "Reporting City", style: "webgridwrap"),
grdLocation.Column("vendor_report_name", "Vendor Reporting City", style: "webgridwrap"),
grdLocation.Column("location_is_active", "Active Status", style: "webgridwrap"),
grdLocation.Column("created_by", "Created By", style: "webgridwrap"),
grdLocation.Column("created_on", "Created On", style: "webgridwrap"),
grdLocation.Column("updated_by", "Updated By", style: "webgridwrap"),
grdLocation.Column("updated_on", "Updated On", style: "webgridwrap")

)
)
}
</div>

Controller....

public class LocationController : Controller
{
LocationModel lm;
[HttpGet]
public ActionResult Index()
{
lm = new LocationModel();

lm.location_name = Session["txtlocationname"] == null ? "" : Session["txtlocationname"].ToString();
lm.location_code = Session["txtlocationcode"] == null ? "" : Session["txtlocationcode"].ToString();
lm.country_name = Session["txtcountryname"] == null ? "" : Session["txtcountryname"].ToString();
lm.location_destination = Session["txtdestinationname"] == null ? "" : Session["txtdestinationname"].ToString();

var GridData = lm.Get_LocationData(lm);
return View(GridData);
}
[HttpPost]
public ActionResult Index(string Command, FormCollection frm)
{
Session["txtlocationname"] = frm["txtlocationname"];
Session["txtlocationcode"] = frm["txtlocationcode"];
Session["txtcountryname"] = frm["txtcountryname"];
Session["txtdestinationname"] = frm["txtdestinationname"];
switch (Command)
{
case "Search":
lm = new LocationModel();
lm.location_name = frm["txtlocationname"] == null ? "" : frm["txtlocationname"];
lm.location_code = frm["txtlocationcode"] == null ? "" : frm["txtlocationcode"];
lm.country_name = frm["txtcountryname"] == null ? "
reshmatces 2-Apr-14 1:02am    
i used Session and value of textbox and on paging value of text box too maintain but we want some other ways....
Raul Iloc 2-Apr-14 1:17am    
I did not used WebGrid, but I am using in my MVC projects jqQrid, and I recommend this grid to be used. You can see details about it in my next article: http://www.codeproject.com/Articles/594150/MVC-Basic-Site-Step-jqGrid-In

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900