mvc4 empty().html,MVC4 / Razor-需要将编辑器框设置为空

本文探讨了在ASP.NET MVC应用中如何使编辑数量框默认为空的问题。介绍了几种解决方案,包括在控制器中设置模型属性为空、使用jQuery清除值、将模型属性类型更改为可空类型以及在视图中清除表单状态。
摘要由CSDN通过智能技术生成

I have the following code...

@using (Html.BeginForm())

{

LocationQuantityPick Qty.

@foreach (FullInventory inv in Model)

{

@Html.DisplayFor(m => inv.InventoryLocationName)@Html.DisplayFor(m => inv.Quantity)@Html.EditorFor(m => inv.Quantity)

}

Pick

}

I would like the "EditorFor" Quantity to be an empty box. It automatically puts the Quantity value in the Quantity box as I would expect. However, I want the Quantity text box to be empty by default when the page loads.

Talk1:

I can see that the use case is to show current quantity and eventually change it in the text box, but wouldn't it be better to just show the editor box with the existing value? leaving it like it is will leave the quantity unchanged, chaning it will change it...

Solutions1

You could either set the model.Quantity to be empty in your controller before you render the view or do it via jquery on the page once the page loads:

$('.tableList tbody .Quantity').val('');

Solutions2

Try doing the following:

@Html.EditorFor(m => inv.Quantity, new { @Value = "" })

Talk1:

I do not think this tactic works if you already have a value. Anyway, either way, it means you end up with 2 "value" attributes. One of them capitalised. It may be a dirty fix when you need to do something quickly but it is not recommended. It may be excessive but I blank them with jquery.

Solutions3

Make Quantity a nullable type in your model, e.g.

int? Quantity { get; set;}

Talk1:

That is going to change the database model though. It seems overkill to make a form work correctly.

Solutions4

If your model holds some value, clear them in the controller before returning the View using

ModelState.Clear();

To make sure the browser doesn't populate the previous values try this,

@Html.EditorFor(model => model.MyField,new { autocomplete = "off" }))

Let me know if it helps ! Cheers.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值