从ViewBag设置文本输入的值? ASP.NET MVC5
发布时间:2020-12-16 07:34:29 所属栏目:asp.Net 来源:网络整理
导读:我从我的控制器传递一个变量ViewBag.crimeRef并使用它来显示一个工作正常的消息: @if(ViewBag.crimeRef != null){ div class="alert alert-dismissable alert-danger" button type="button" class="close" data-dismiss="alert"×/button Sorry,there are n
我从我的控制器传递一个变量ViewBag.crimeRef并使用它来显示一个工作正常的消息:
@if(ViewBag.crimeRef != null) { <div class="alert alert-dismissable alert-danger"> <button type="button" class="close" data-dismiss="alert">×</button> Sorry,there are no matching results for the crime reference <strong>@ViewBag.crimeRef</strong>. Please try searching again. </div> } 我想我也可以使用相同的方法来填充输入的值,但是我得到的是CS1002 :;预期错误: <input type="text" class="form-control" maxlength="11" id="crimeRef" name="crimeRef" placeholder="Crime reference" @if(ViewBag.crimeRef != null) { value="@ViewBag.crimeRef" }> 无论如何我可以使用ViewBag变量填充输入的值吗? 非常感谢 解决方法
答案1: –
<input type="text" class="form-control" maxlength="11" id="crimeRef" name="crimeRef" placeholder="Crime reference" value="@(ViewBag.crimeRef ?? String.Empty)" > 答2: – 我在这里包括Nick的答案,因为这是可接受的答案,因此它将帮助其他stackoverflow用户在一个地方获得他们的问题的答案. 条件语句实际上是在MVC4中构建到Razor中的: Conditional HTML Attributes using Razor MVC3 所以简单地使用这个: <input type="text" class="form-control" maxlength="11" id="crimeRef" name="crimeRef" placeholder="Crime reference" value="@(ViewBag.crimeRef)"> 如果ViewBag.crimeRef不为null,则只会呈现value属性 – 完美! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – C#MVC对浏览器的即时响应?
- ASP.net上的URL路由,需要从url获取参数
- asp.net – 从MVC控制器返回不同的视图
- asp.net-2.0 – 我想阻止ASP.NET GridView对enter按钮做出反
- asp.net-web-api – 使用swashbuckle api文档的http basic
- asp.net – Intuit合作伙伴平台(IPP)QuickBooks Online(QBO
- asp.net-mvc – 如何在ASP.NET MVC中存根User.Identity.Nam
- 在asp.net core 2.0中使用identityserver4时的无限认证循环
- asp.net-mvc – ASP.NET MVC – 在ActionFilter中访问控制器
- asp.net-mvc-4 – ModelBinder和子模型
推荐文章
站长推荐
- asp.net-mvc – 如何从VOID方法重定向到MVC3中的
- asp.net – Web API 2 – ApiController.Interna
- .net – MVC – 是模型查看还是控制器查看?
- asp.net – 使用Jquery访问hiddenfield
- asp.net-mvc – 使用嵌套显示模板时如何防止Razo
- 单元测试 – Stubbing或Mocking ASP.NET Web API
- asp.net – 在Application_BeginRequest中设置会
- asp.net – linq to sql update standard
- asp.net – 按钮单击在更新面板中不起作用
- 实体框架 – ASP.net身份,IoC和共享DbContext
热点阅读