10.TextBox , Hidden %=Html.TextBox(input1) % %=Html.TextBox(input2,Model.CategoryName,new{ @style = width:300px; }) % %=Html.TextBox(input3, ViewData[Name],new{ @style = width:300px; }) % %=Html.TextBoxFor(a = a.CategoryName, new { @style
<p><strong>10.TextBox , Hidden </strong></p><p><%=Html.TextBox("input1") %></p><p><%=Html.TextBox("input2",Model.CategoryName,new{ @style = "width:300px;" }) %></p><p><%=Html.TextBox("input3", ViewData["Name"],new{ @style = "width:300px;" }) %></p><p><%=Html.TextBoxFor(a => a.CategoryName, new { @style = "width:300px;" })%></p><p>生成结果: </p><p><input id="input1" name="input1" type="text"value="" /></p><p><input id="input2" name="input2" style="width:300px;" type="text"value="Beverages" /></p><p><input id="input3" name="input3" style="width:300px;" type="text"value="" /></p><p><input id="CategoryName" name="CategoryName" style="width:300px;" type="text"value="Beverages" /></p><p><strong>11.TextArea</strong></p><p><%=Html.TextArea("input5", Model.CategoryName, 3, 9,null)%></p><p><%=Html.TextAreaFor(a => a.CategoryName, 3, 3, null)%></p><p>生成结果: </p><p><textarea cols="9" id="input5" name="input5" rows="3">Beverages</textarea></p><p><textarea cols="3" id="CategoryName" name="CategoryName" rows="3">Beverages</textarea></p><p><strong>12.CheckBox</strong></p><p><%=Html.CheckBox("chk1",true) %></p><p><%=Html.CheckBox("chk1", new { @class="checkBox"}) %></p><p><%=Html.CheckBoxFor(a =>a.IsVaild, new { @class = "checkBox" })%></p><p>生成结果: </p><p><input checked="checked" id="chk1" name="chk1" type="checkbox"value="true" /></p><p><input name="chk1" type="hidden"value="false" /></p><p><input class="checkBox" id="chk1" name="chk1" type="checkbox"value="true" /></p><p><input name="chk1" type="hidden"value="false" /></p><p><input checked="checked"class="checkBox" id="IsVaild" name="IsVaild" type="checkbox"value="true" /></p><p><input name="IsVaild" type="hidden"value="false" /></p><p>13.<strong>ListBox</strong></p><p><%=Html.ListBox("lstBox1",(SelectList)ViewData["Categories"])%></p><p><%=Html.ListBoxFor(a => a.CategoryName, (SelectList)ViewData["Categories"])%></p><p> 生成结果:</p><p><select id="lstBox1" multiple="multiple" name="lstBox1"></p><p><option value="1">Beverages</option></p><p><option value="2">Condiments</option></p><p><option selected="selected"value="3">Confections</option></p><p><option value="4">Dairy Products</option></p><p><option value="5">Grains/Cereals</option></p><p><option value="6">Meat/Poultry</option></p><p><option value="7">Produce</option></p><p><option value="8">Seafood</option></p><p></select></p><p><select id="CategoryName" multiple="multiple" name="CategoryName"></p><p><option value="1">Beverages</option></p><p><option value="2">Condiments</option></p><p><option value="3">Confections</option></p><p><option value="4">Dairy Products</option></p><p><option value="5">Grains/Cereals</option></p><p><option value="6">Meat/Poultry</option></p><p><option value="7">Produce</option></p><p><option value="8">Seafood</option></p><p></select></p><p><strong>14.DropDownList</strong></p><p><strong><%= Html.DropDownList("ddl1", (SelectList)ViewData["Categories"], "--Select One--")%></strong></p><p><strong><%=Html.DropDownListFor(a => a.CategoryName, (SelectList)ViewData["Categories"], "--Select One--", new { @class = "dropdownlist" })%></strong></p><p><strong> 生成结果:</strong></p><p><strong><select id="ddl1" name="ddl1"></strong></p><p><strong><option value="">--Select One--</option></strong></p><p><strong><option value="1">Beverages</option></strong></p><p><strong><option value="2">Condiments</option></strong></p><p><strong><option selected="selected"value="3">Confections</option></strong></p><p><strong><option value="4">Dairy Products</option></strong></p><p><strong><option value="5">Grains/Cereals</option></strong></p><p><strong><option value="6">Meat/Poultry</option></strong></p><p><strong><option value="7">Produce</option></strong></p><p><strong><option value="8">Seafood</option></strong></p><p><strong></select> </strong></p><p><strong><select class="dropdownlist" id="CategoryName" name="CategoryName"></strong></p><p><strong><option value="">--Select One--</option></strong></p><p><strong><option value="1">Beverages</option></strong></p><p><strong><option value="2">Condiments</option></strong></p><p><strong><option value="3">Confections</option></strong></p><p><strong><option value="4">Dairy Products</option></strong></p><p><strong><option value="5">Grains/Cereals</option></strong></p><p><strong><option value="6">Meat/Poultry</option></strong></p><p><strong><option value="7">Produce</option></strong></p><p><strong><option value="8">Seafood</option></strong></p><p><strong></select></strong></p><p><strong>15.<strong>Partial 视图模板</strong></strong></p><p>webform里叫自定义控件。功能都是为了复用。但使用上自定义控件真的很难用好。</p><p><% Html.RenderPartial("DinnerForm"); %> 看清楚了没有等号的。</p>
16 mvc中的模糊查询和精确查询:
var talk = dbTalk.talk.Where(m => m.name.IndexOf(keyWord)>=0);//模糊查询
dbTalk.talk.Where(m => m.name.Equals(keyWord) == true);//精确查询
应用:
在控制器中的Action:
public ActionResult search()
{
string keyWord = Request.Form["keyWord"];
var talk = dbTalk.talk.Where(m => m.name.IndexOf(keyWord)>=0);//dbTalk.talk.Where(m => m.name.Equals(keyWord) == true);//
if (talk == null)
{ ViewData["error"] = "未找到相关信息!"; }
else
{ ViewData["error"] = "Yes!";}
ViewData.Model = talk; //视图页面数据
return View();
}
view中代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="MvcApplication1.Models" %>//引入models
<% foreach (talk item in (IEnumerable)ViewData.Model)//转换为你想要的类型(ViewData.Model为控制器中传过来的数据)
{ %>