asp.net mvc 包含了一个 html 的助手类在哪里,ASP.Net MVC助手

在asp.net web表单中,开发人员正在使用工具箱来在任何特定的页面上添加控件。 但是,在asp.net mvc应用程序中,没有工具箱可用于在视图上拖放html控件。 在asp.net mvc应用程序中,如果想创建一个视图,它应该包含html代码。 所以那些刚接触mvc的开发者,特别是在网页表单的背景下,发现这个有点难。

为了克服这个问题,asp.net mvc提供了htmlhelper类,它包含不同的方法来帮助你编程创建html控件。 所有的htmlhelper方法都会生成html并以字符串形式返回结果。 最终的html是由这些函数在运行时生成的。 htmlhelper类用于生成ui,不应该在控制器或模型中使用。

有不同类型的帮手方法。

createinputs - 为文本框和按钮创建输入。

createlinks - 创建基于来自路由表的信息的链接。

createforms - 创建表单标签,可以回发到动作,或回发到另一个控制器上的操作。

如果有看过前面视图教程文章中(项目:mvcsimpleapp)employeecontroller控制器的index动作生成的视图,将看到以html开始的操作符前缀,如html.actionlink和html.displaynamefor等,如下面的代码所示。

@model ienumerable

@{

layout = null;

}

index

@html.actionlink("create new", "create")

@html.displaynamefor(model => model.name)

@html.displaynamefor(model => model.joiningdate)

@html.displaynamefor(model => model.age)

@foreach (var item in model) {

@html.displayfor(modelitem => item.name)

@html.displayfor(modelitem => item.joiningdate)

@html.displayfor(modelitem => item.age)

@html.actionlink("edit", "edit", new { id = item.id }) |

@html.actionlink("details", "details", new { id = item.id }) |

@html.actionlink("delete", "delete", new { id = item.id })

}

这个html是从viewpage基类继承的一个属性。所以,它在所有的视图中都可用,并返回一个名为htmlhelper的实例。

我们来看看一个简单的例子,让用户可以编辑员工信息。 因此,此编辑操作将使用大量不同的html助手。

如果看上面的代码,会在最后部分看到下面的html helper方法 -

@html.actionlink("edit", "edit", new { id = item.id })

在actionlink助手中,第一个参数是“edit”链接,第二个参数是控制器中的动作方法,也是“edit”,第三个参数id是要编辑的员工编号。

我们通过添加静态列表来更改employeecontroller类,并使用以下代码更改索引操作。

public static list emplist = new list{

new employee{

id = 1,

name = "allan",

joiningdate = datetime.parse(datetime.today.tostring()),

age = 23

},

new employee{

id = 2,

name = "carson",

joiningdate = datetime.parse(datetime.today.tostring()),

age = 45

},

new employee{

id = 3,

name = "carson",

joiningdate = datetime.parse(datetime.today.tostring()),

age = 37

},

new employee{

id = 4,

name = "laura",

joiningdate = datetime.parse(datetime.today.tostring()),

age = 26

},

};

public actionresult index(){

var employees = from e in emplist

orderby e.id

select e;

return view(employees);

}

下面来更新编辑操作。您将看到两个编辑操作,一个用于get,一个用于post。这里更新get的编辑操作,其中只有参数中的id,如下面的代码所示。

// get: employee/edit/5

public actionresult edit(int id){

list emplist = getemployeelist();

var employee = emplist.single(m => m.id == id);

return view(employee);

}

现在,我们知道有编辑的动作,但是没有任何对应此动作的视图。 所以还需要添加一个视图(view)。 为此,请右键单击edit操作,然后选择添加视图。从“模板”下拉列表中选择edit,从“模型”下拉列表中选择“employee” -

6063.html

以下是edit视图中的默认实现。参考以下示例代码 -

@model mvcsimpleapp.models.employee

@{

layout = null;

}

edit

@using (html.beginform())

{

@html.antiforgerytoken()

employee

@html.validationsummary(true, "", new { @class = "text-danger" })

@html.hiddenfor(model => model.id)

@html.labelfor(model => model.name, htmlattributes: new { @class = "control-label col-md-2" })

@html.editorfor(model => model.name, new { htmlattributes = new { @class = "form-control" } })

@html.validationmessagefor(model => model.name, "", new { @class = "text-danger" })

@html.labelfor(model => model.joiningdate, htmlattributes: new { @class = "control-label col-md-2" })

@html.editorfor(model => model.joiningdate, new { htmlattributes = new { @class = "form-control" } })

@html.validationmessagefor(model => model.joiningdate, "", new { @class = "text-danger" })

@html.labelfor(model => model.age, htmlattributes: new { @class = "control-label col-md-2" })

@html.editorfor(model => model.age, new { htmlattributes = new { @class = "form-control" } })

@html.validationmessagefor(model => model.age, "", new { @class = "text-danger" })

}

@html.actionlink("back to list", "index")

html.beginform非常有用,因为它使您能够更改url,更改方法等。

在上面的代码中,看到另一个html助手:@ html.hiddenfor,它用于生成隐藏的字段。

mvc框架足够聪明地发现这个id字段在模型类中,因此它需要防止被编辑编修改,这就是为什么它被标记为隐藏字段的原因。

html.labelfor html助手在屏幕上创建标签。如果在进行更改时错误地输入了任何内容,则html.validationmessagefor助手将显示正确的错误消息。

还需要更改post的编辑操作,需要更新员工信息时,它就会调用这个动作。参考以下代码 -

// post: employee/edit/5

[httppost]

public actionresult edit(int id, formcollection collection)

{

try

{

// todo: add update logic here

list emplist = getemployeelist();

var employee = emplist.single(m => m.id == id);

if (tryupdatemodel(employee))

{

//to do:- database code

return redirecttoaction("index");

}

return view(employee);

}

catch

{

return view();

}

}

让我们运行这个应用程序,并请求以下url:http://localhost:64466/employee员工。将会看到以下输出。

6063.html

点击任何特定员工的编辑链接,以点击员工编号为1编辑链接为示例,您将看到以下视图显示结果。

6063.html

将年龄从23岁改为29岁,然后点击“保存”按钮,然后会在index视图中看到更新的年龄。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值