!在mvc4 razor中回复... [英] ! is post back...in mvc4 razor

查看:85
本文介绍了!在mvc4 razor中回复...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在普通Asp.net中我们在!ispostback中编写代码并在服务器端编写代码...例如..



In Normal Asp.net We Write Code in !ispostback and write code in it server side...for example..

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

    }





}



怎么样在mvc4 razor中建立!ispostback概念。哪里..... ??????

回复..提前谢谢..



}

how to Establish !ispostback concept in mvc4 razor. and where.....??????
reply..thanks in advance..

推荐答案

用户管理动作MVC与ASP.NET不同。



让我们通过一个例子进行解释,假设用户希望通过链接激活Édit页面或者索引页面上的按钮。



1.响应用户操作,控制器中必须有一个方法(将通过使用GET从客户端调用)名为 ViewResult Edit()用于创建Model并返回 ViewResult ,它将呈现视图并将其发送到客户端浏览器。所以这个Edit()方法仅针对第一个GET调用(类似于IsPostback等于false)!



2.然后浏览器将显示编辑视图(页面) )并且用户可以使用一些收集用户输入的控件,最后按保存提交按钮,首先会激活客户端验证然后,如果客户端验证将成功,它将生成回发到服务器。



3.在您的控制器上应该存在具有相同名称的第二种方法 ActionResult Edit (EditModel模型)但它们应该有一些参数(通常是使用的模型对象),这个方法必须指定 [HttpPost] 属性,这个方法将在上面的第2步中从客户端调用。这种方法通常应该对整个用户输入进行一些逻辑验证,然后尝试将输入数据保存到数据库中,还应该管理可能的错误(这样可以从其代码中生成不同的操作结果)。



您可以在我的下一篇文章中看到示例: MVC基本站点:第1步 - 多语言站点骨架 [ ^ ]
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[^]


这篇关于!在mvc4 razor中回复...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆