yii2 模型中set_模型

    1. 表单和HTML辅助方法
  • Methed

通常情况下GET用于读取操作,POST用于更新、创建、删除。GET会组合参数在url地址中。

Html.BeginForm
@using  (Html.BeginForm("Search","Home",FormMethod,Get)){
<input type="text" name="q"/>
<input type="submit" value="Search"/>
}
    • 自动编码
@Html.TextArea("text","hello word")
等同<textarea cols="20" id="text" name  ="text" rows="2" >hello word</textarea>
    • 辅助方法
@using  (Html.BeginForm("Search","Home",FormMethod,Get,
New  {target="_blank",@class="editForm"  data_validatable=true })){
<input type="text" name="q"/>
<input type="submit" value="Search"/>
}
生成为<form action="/Home/Search" method="get"  target="_blank" class="editForm" data-validatable="true">

Class 关键字使用“@”前缀;带有连接字符属性是无效的,使用下划线代替,渲染时自动转为连接字符。

    • 输入元素
@Html.TextBox("Title",Mode.Title)
@Html.TextArea("text",Mode.Content,10,80,null)
@Html.Lablel("genreId") for属性点击lable自动聚焦到for的输入控件。
@Html.DropDownList 单选
@Html.ListBox 多选
@Html.Hidden("hid","123");隐藏
@Html.Password("userpwd");密码
@Html.RadioButton("color","red");单选
@Html.RadioButton("color","blue",true);
@Html.RadioButton("color","green");
@Html.CheckBox("IsChk");多选

绑定下拉列表

c991a3ab13d2966c1cc71afeb254b3e1.png
绑定下拉列表

@Html.validationMessage

控制器录入错误描述,ModelState.AddModelError("Title"," this is a test erro");

视图中使用@Html.validationMessage("Title")来显示错误,这条消息只有在"Title"在模型中出现错误时才出现,

也可调用@Html.validationMessage("Title","New erro text ")重写错误提示。

    • 辅助方法、模型和视图数据
控制器:public  ActionResult Edit(int? id)
 {
 ViewBag.LmPrice = 10.0;
Return view();
 }
视图:@Html.TextBox("LmPrice")
Html:<input  id="LmPrice" name="LmPrice" type="text"  value="" /> 
对象数据
控制器:public  ActionResult Edit(int? id)
 {
 ViewBag.LmData = new  LmData {  name=test, Price:10.0}
Return view(LmData );
 }
视图:@Html.TextBox("LmData.name")
Html:<input  id="LmData_nam" name="LmData.nam" type="text" value="" /> 
    • 强类型辅助方法
这些辅助方法名包含"For"后缀,生成的HTML上面的相同,但是lambda有职能感知,编译检查,代码重构。
@model  RentHousePro.Models.HouseInfo
 @Html.DisplayNameFor(model => model.price)
 @foreach (var item in Model) {
 @Html.DisplayFor(modelItem => item.price)
}

模板辅助方法

Html.Display/Html.Editor 对应的强类型Html.DisplayForModel/Html.EditorForModel

bb6e983a302360044ccdd8a19a5d052d.png
    • 渲染辅助模块
@Html.ActionLink("Link  Text","AnotherAction"); //相同控制器
@Html.ActionLink("Link  Text","Action","Controller");  //不同控制器
@Html.ActionLink("Link  Text","Action","Controller",new{id=123},null);  //不同控制器带参数
@Html.RouteLink("Link  Text",new {action  ="anotherAction"}); //路由名称
@Url.Action("Browse","Store",new  {genre="jazz"},null)//URL辅助方法,不反悔锚标签
@Url.RouteUrl()
@Url.Content("~/Script/xx.js")//相对路径转为绝对路径
@Html.Partial()//将部分视图渲染成字符串
@{Html.RenderPartial();}//不返回,直接写入响应输出流。
@Html.Action("Menu")//执行单独的控制器并显示结果

f01ae672ec7bda9e1597f4b0e2dbd991.png

@Html.RenderAction("Menu")//执行单独的控制器并显示结果,直接写入响应流。

PS 模型中要写KEY

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace RentHousePro.Models
{
 public class HouseInfo
 {
  [Key]
 public int hid { get; set; }
 public decimal? price { get; set; }
 public string conent { get; set; }
 public string keyword { get; set; }
 public string type { get; set; }
 public string ctype { get; set; }
 public string no { get; set; }
 public int? aircondition { get; set; }
 public int? toilet { get; set; }
 public string floor { get; set; }
 public string tel { get; set; }
 public string paycost { get; set; }
 public int? istop { get; set; }
 public int? state { get; set; }
 public virtual List<HousePicRelation> HousePics { get; set; }
 }
 
 public class HousePicRelation
 {
 [Key]
 public int hpid { get; set; }
 public int hid { get; set; }
 public string Src { get; set; }
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值