Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 RAZOR中的文本框没有激活验证?_Asp.net Mvc 3_Razor Grid - Fatal编程技术网

Asp.net mvc 3 RAZOR中的文本框没有激活验证?

Asp.net mvc 3 RAZOR中的文本框没有激活验证?,asp.net-mvc-3,razor-grid,Asp.net Mvc 3,Razor Grid,1下面我已经发布了我的视图总编码。这里验证不是在文本框上启动。我不知道如何解决这个问题。此视图正在执行。如果我没有在文本框中输入文本而按下搜索文本框,则不会进行验证。请告诉我是否必须使用文本框或文本框进行验证。我是mvc3的新手。请告诉我解决办法 @model IEnumerable< ShoppingCart.Models.ShoppingClass> @{ ViewBag.Title = "Display"; }

1下面我已经发布了我的视图总编码。这里验证不是在文本框上启动。我不知道如何解决这个问题。此视图正在执行。如果我没有在文本框中输入文本而按下搜索文本框,则不会进行验证。请告诉我是否必须使用文本框或文本框进行验证。我是mvc3的新手。请告诉我解决办法

    @model  IEnumerable< ShoppingCart.Models.ShoppingClass>
        @{
            ViewBag.Title = "Display";

        }
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

@Html.ValidationSummary(true)
 @using (Html.BeginForm("Display","Home", FormMethod.Post, new { id = "loginForm" }))


        {

            //for (int i = 0; i < 1; i++)
            //{
            <table><tr>o<td> @Html.Label("BrandName")</td>
            <td>@Html.TextBox("BrandName") <div> @Html.ValidationMessage("BrandName")</div></td>
         <td></td></tr></table>



    @*    <table><tr><td>

         @Html.LabelFor(o=>o[i].BrandName)</td>
        <td>@Html.EditorFor(o => o[i].BrandName) <div>@Html.ValidationMessageFor(o => o[i].BrandName)</div></td>
        <td></td></tr></table>*@



          //  }
               <input type="submit" value="Search" name="Search" />
        }



  @{
    var grid = new WebGrid(source: Model, defaultSort: "Drug_Code", rowsPerPage: 20);
 <div id="grid">
    @grid.GetHtml(tableStyle: "listing-border", headerStyle: "gridhead", footerStyle: "paging", rowStyle: "td-dark", alternatingRowStyle: "td-light",
       columns: grid.Columns(
                   grid.Column("GenericName", format: @<text>@item.GenericName</text>),
                   grid.Column("BrandName", format: @<text>@item.BrandName</text>),
                   grid.Column("Purchaseqty", format: @<text>@item.Purchaseqty</text>),
                   grid.Column("Purchaseprice", format: @<text>@item.Purchaseprice</text>),
                   grid.Column("Drug_Code", format: @<text>@item.Drug_Code</text>),
                   grid.Column(header: "", format: (item) => Ajax.ActionLink("Add to Cart", "ADDTOCART",
                   new { brandname = @item.BrandName, purchaseqty = @item.Purchaseqty, drugcode = @item.Drug_Code }, new AjaxOptions { HttpMethod = "Post", OnSuccess = "ADDTOCART" }))

                                                                                                )
   </div>

      }

@model IEnumerable
@{
ViewBag.Title=“显示”;
}
@Html.ValidationSummary(true)
@使用(Html.BeginForm(“Display”、“Home”、FormMethod.Post、new{id=“loginForm”}))
{
//对于(int i=0;i<1;i++)
//{
o@Html.Label(“品牌名称”)
@Html.TextBox(“BrandName”)@Html.ValidationMessage(“BrandName”)
@*    
@LabelFor(o=>o[i].BrandName)
@Html.EditorFor(o=>o[i].BrandName)@Html.ValidationMessageFor(o=>o[i].BrandName)
*@
//  }
}
@{
var grid=new-WebGrid(来源:Model,defaultSort:“药品代码”,rowsPerPage:20);
@GetHtml(表样式:“列表边框”,标题样式:“gridhead”,页脚样式:“分页”,行样式:“td深色”,交替行样式:“td浅色”,
列:grid.columns(
grid.Column(“GenericName”,格式:@@item.GenericName),
grid.Column(“BrandName”,格式:@@item.BrandName),
grid.Column(“Purchaseqty”,格式:@@item.Purchaseqty),
grid.Column(“Purchaseprice”,格式:@@item.Purchaseprice),
grid.Column(“药物代码”,格式:@@item.Drug\u代码),
grid.Column(标题:“”,格式:(item)=>Ajax.ActionLink(“添加到购物车”、“添加到购物车”,
新的{brandname=@item.brandname,purchaseqty=@item.purchaseqty,drugcode=@item.druge\u Code},新的AjaxOptions{HttpMethod=“Post”,OnSuccess=“ADDTOCART”})
)
}

编辑:添加了一个更完整的示例

从上面的评论来看,我不确定它是否能让您满意,但我认为您的问题的根源在于,您正试图在不需要IEnumerable的地方使用IEnumerable模型。我知道您的网格需要它,但如果您将网格或搜索框放在PartialView中会怎么样?PartialView可能有它自己的功能如果你有自己的模型,那么你就不需要把你的搜索框放进一个不太合适的模型中

按照我的理解,您实际上并没有将数据传递到搜索框中。您只是在搜索框中使用模型,以便获得字段验证。如果这是错误的,请告诉我

编辑视图,使其如下所示:

@model  IEnumerable< ShoppingCart.Models.ShoppingClass>
    @{
        ViewBag.Title = "Display";
    }
   <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript">    </script>
   <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

@Html.ValidationSummary(true)

@{Html.RenderPartial("_Search", Model.First());} //Need to make sure Model.First() actually exists, or you'll get error when it doesn't
//If you change your partial view to have a view model with just brand name, call it like this:
//@Html.RenderPartial("_Search", new _SearchViewModel())
//If you're only using the view model for required field validation on the return trip, then you don't actually need to pass data down to it.

@{
  var grid = new WebGrid(source: Model, defaultSort: "Drug_Code", rowsPerPage: 20);
 <div id="grid">
@grid.GetHtml(tableStyle: "listing-border", headerStyle: "gridhead", footerStyle: "paging", rowStyle: "td-dark", alternatingRowStyle: "td-light",
   columns: grid.Columns(
               grid.Column("GenericName", format: @<text>@item.GenericName</text>),
               grid.Column("BrandName", format: @<text>@item.BrandName</text>),
               grid.Column("Purchaseqty", format: @<text>@item.Purchaseqty</text>),
               grid.Column("Purchaseprice", format: @<text>@item.Purchaseprice</text>),
               grid.Column("Drug_Code", format: @<text>@item.Drug_Code</text>),
               grid.Column(header: "", format: (item) => Ajax.ActionLink("Add to Cart", "ADDTOCART",
               new { brandname = @item.BrandName, purchaseqty = @item.Purchaseqty, drugcode = @item.Drug_Code }, new AjaxOptions { HttpMethod = "Post", OnSuccess = "ADDTOCART" }))

                                                                                                )
</div>

  }

@model IEnumerable
@{
ViewBag.Title=“显示”;
}
@Html.ValidationSummary(true)
@{Html.RenderPartial(“_Search”,Model.First());}//需要确保Model.First()确实存在,否则当它不存在时,您将得到错误
//如果将局部视图更改为仅具有品牌名称的视图模型,请这样称呼它:
//@Html.RenderPartial(“_Search”,新的_SearchViewModel())
//如果在回程中只使用视图模型进行所需的字段验证,那么实际上不需要将数据传递给它。
@{
var grid=new-WebGrid(来源:Model,defaultSort:“药品代码”,rowsPerPage:20);
@GetHtml(表样式:“列表边框”,标题样式:“gridhead”,页脚样式:“分页”,行样式:“td深色”,交替行样式:“td浅色”,
列:grid.columns(
grid.Column(“GenericName”,格式:@@item.GenericName),
grid.Column(“BrandName”,格式:@@item.BrandName),
grid.Column(“Purchaseqty”,格式:@@item.Purchaseqty),
grid.Column(“Purchaseprice”,格式:@@item.Purchaseprice),
grid.Column(“药物代码”,格式:@@item.Drug\u代码),
grid.Column(标题:“”,格式:(item)=>Ajax.ActionLink(“添加到购物车”、“添加到购物车”,
新的{brandname=@item.brandname,purchaseqty=@item.purchaseqty,drugcode=@item.druge\u Code},新的AjaxOptions{HttpMethod=“Post”,OnSuccess=“ADDTOCART”})
)
}
创建局部视图(\u搜索):

@model ShoppingCart.Models.ShoppingClass
//您还可以创建一个只有品牌名称的不同视图模型,并以另一种方式传递数据
//有关该视图,请参见代码中的示例
@Html.ValidationSummary(true)
@使用(Html.BeginForm(“Display”、“Home”、FormMethod.Post、new{id=“loginForm”}))
{
@LabelFor(o=>o.BrandName)
@EditorFor(o=>o.BrandName)@Html.ValidationMessageFor(o=>o.BrandName)
}

您为文本框设置了什么验证规则?您确定这样做了吗?您的模型是什么样子的?您是否使用[StringLength(50,ErrorMessage=“最大长度为50个字符”)]或[Required(ErrorMessage=“This field is Required”)]@FSou1:[Required(ErrorMessage=“BrandNamest is Required”)]公共字符串BrandName{get;set;}我在中使用了提到的必需验证model@user1304444:是的,我用过它…我注意到你有TextBoxFor(o=>o.BrandName)和LabelFor(o=>o.BrandName[I])你是故意遗漏枚举数的吗?如果你想要模型中的每一项,你需要一个循环。你告诉我将webgrid放在部分视图中。好的,我做到了。现在如何将部分视图调用到当前视图页面..我理解的是正确的..我不完全理解你在问什么…你想知道如何呈现部分吗al视图?类似以下内容:@Html.RenderPartial(“PartialViewName”,model)…查看此内容后,可能只会将搜索框放在部分视图中,而不会放在web gri中
@model ShoppingCart.Models.ShoppingClass
//You could also create a different View Model that just has brand name, and pass the data down another way
//See examples in the code for the view

@Html.ValidationSummary(true)
@using (Html.BeginForm("Display","Home", FormMethod.Post, new { id = "loginForm" }))
{
    <table><tr><td>

     @Html.LabelFor(o=>o.BrandName)</td>
    <td>@Html.EditorFor(o => o.BrandName) <div>@Html.ValidationMessageFor(o => o.BrandName)</div></td>
    <td></td></tr></table>

    <input type="submit" value="Search" name="Search" />
}