C# 根据editorFor环境更改提交按钮的功能
我在我的网站上有一个编辑表单页面,允许用户编辑数量,在这个页面上我使用了editorFor,看起来像这样C# 根据editorFor环境更改提交按钮的功能,c#,html,jquery,asp.net-mvc,razor,C#,Html,Jquery,Asp.net Mvc,Razor,我在我的网站上有一个编辑表单页面,允许用户编辑数量,在这个页面上我使用了editorFor,看起来像这样 @Html.EditorFor(model => model.item_qty, new { htmlAttributes = new { min = 0, @class = "form-control" } }) <input type="submit" value="Save" class="btn" /> <input type="submit" value
@Html.EditorFor(model => model.item_qty, new { htmlAttributes = new { min = 0, @class = "form-control" } })
<input type="submit" value="Save" class="btn" />
<input type="submit" value="Save" onclick="confirm()" class="btn" />
我有一个提交按钮,看起来像这样
@Html.EditorFor(model => model.item_qty, new { htmlAttributes = new { min = 0, @class = "form-control" } })
<input type="submit" value="Save" class="btn" />
<input type="submit" value="Save" onclick="confirm()" class="btn" />
我想这样做,如果用户增加数量,它将继续运行post方法。但另一方面,如果用户要减少数量,我希望save按钮如下所示
@Html.EditorFor(model => model.item_qty, new { htmlAttributes = new { min = 0, @class = "form-control" } })
<input type="submit" value="Save" class="btn" />
<input type="submit" value="Save" onclick="confirm()" class="btn" />
让用户在运行post之前进行确认
如何根据用户在editorFor中键入的内容更改保存按钮
这是我的整个查看页面
@model PIC_Program_1._0.Models.JODetails
@using PIC_Program_1._0.Models
@{
ViewBag.Title = "Edit";
PIC_Program_1_0Context db = new PIC_Program_1_0Context();
var currentData = db.JODetails.AsNoTracking().FirstOrDefault(j => j.ID == Model.ID);
Component comp = db.Components.Find(Model.ComponentID);
Item i = db.Items.Find(Model.ItemID);
}
<script type="text/javascript">
function clicked(e) {
if(@i != null ) {
var itemDiff = // model - new editorfor value;
if (!confirm('Are you sure? Doing this will reduce item ' + @i.ItemID + ' future stock to ' + itemDiff))e.preventDefault();
}
}
function OnChangeEvent(){
alert("value is changed");
var itemQty = $('#itemQTY').val();
if (itemQty < @Model.item_qty) {
btn.Attributes.Add("onclick", "clicked(event)");
}
}
</script>
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>JODetails</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
@Html.HiddenFor(model => model.subDetail)
<p style="color:red">@ViewBag.Error</p>
<div class="form-group">
@Html.LabelFor(model => model.ItemID, "ItemID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ItemID", null, "-- Select --", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ItemID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.item_qty, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@if (ViewBag.isValid == false)
{
@Html.TextBoxFor(model => model.item_qty, new { disabled = "disabled", @Value = Model.item_qty, @readonly = "readonly" })
}
else
{
@Html.EditorFor(model => model.item_qty, new { htmlAttributes = new { onchange = "OnChangeEvent()", min = 0, @class = "form-control" @id = "itemQTY"} })
@Html.ValidationMessageFor(model => model.item_qty, "", new { @class = "text-danger" })
}
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn"/>
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@model PIC\u Program\u 1.\u 0.Models.JODetails
@使用PIC_程序_1._0.0模型
@{
ViewBag.Title=“编辑”;
PIC_程序_1_0上下文db=新PIC_程序_1_0上下文();
var currentData=db.JODetails.AsNoTracking().FirstOrDefault(j=>j.ID==Model.ID);
Component comp=db.Components.Find(Model.ComponentID);
Item i=db.Items.Find(Model.ItemID);
}
函数(e){
如果(@i!=null){
var itemDiff=//model-值的新编辑器;
如果(!confirm('你确定吗?这样做会将项目'+@i.ItemID+'未来库存减少到'+itemDiff)),e.preventDefault();
}
}
函数OnChangeEvent(){
警报(“值已更改”);
var itemQty=$('#itemQty').val();
如果(项目数量<@Model.item\u数量){
添加(“onclick”,“clicked(event)”);
}
}
编辑
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
JODetails
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.ID)
@Html.HiddenFor(model=>model.subDetail)
@ViewBag.Error
@LabelFor(model=>model.ItemID,“ItemID”,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownList(“ItemID”,null,“--Select--”,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.ItemID,“,new{@class=“text danger”})
@LabelFor(model=>model.item_数量,htmlAttributes:new{@class=“controllabel col-md-2”})
@如果(ViewBag.isValid==false)
{
@Html.TextBoxFor(model=>model.item_数量,新的{disabled=“disabled”,@Value=model.item_数量,@readonly=“readonly”})
}
其他的
{
@EditorFor(model=>model.item_数量,new{htmlAttributes=new{onchange=“onchangevent()”,min=0,@class=“form control”@id=“itemQTY”})
@Html.ValidationMessageFor(model=>model.item_qty,“,new{@class=“text danger”})
}
}
@ActionLink(“返回列表”、“索引”)
有关更改,您可以在编辑器上进行以下操作:
@Html.EditorFor(model => model.item_qty, new { htmlAttributes = new { onchange = "OnChangeEvent(this)", min = 0, @class = "form-control", @id ="someId" } })
<script type="text/javascript">
function OnChangeEvent(){
var itemQty = $('#someID').val(); //This will give you value of editor for
alert("new value is: " + itemQty);
//Call some controller function here and do your increase/decrease logic and return value so we know what to do. also perform your save in controller too if needed.
@Model.IsChanged = true;
//do other functions here also like change button
//if decrease add below
btn.Attributes.Add("onclick", "confirm()");
//if increase remove and perform save.
}
</script>
@Html.EditorFor(model=>model.item_数量,new{htmlAttributes=new{onchange=“onchangevent(this)”,min=0,@class=“form control”,@id=“someId”})
函数OnChangeEvent(){
var itemQty=$('#someID').val();//这将为您提供编辑器的值
警报(“新值为:“+itemQty”);
//在这里调用一些控制器函数,并执行递增/递减逻辑和返回值,以便我们知道该做什么。如果需要,还可以在控制器中执行保存。
@Model.IsChanged=true;
//这里是否还有其他功能,如更改按钮
//如果减少,请在下面添加
添加(“onclick”、“confirm()”;
//如果增加,请删除并执行保存。
}
您还可以从该方法更改按钮。除非使用blazor,否则没有在服务器级别执行调用事件的好方法,所以javascript是您最好的选择
每个OP的更新答案:
@model PIC_Program_1._0.Models.JODetails
@using PIC_Program_1._0.Models
@{
ViewBag.Title = "Edit";
PIC_Program_1_0Context db = new PIC_Program_1_0Context();
var currentData = db.JODetails.AsNoTracking().FirstOrDefault(j => j.ID == Model.ID);
Component comp = db.Components.Find(Model.ComponentID);
Item i = db.Items.Find(Model.ItemID);
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>JODetails</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
@Html.HiddenFor(model => model.subDetail)
<p style="color:red">@ViewBag.Error</p>
<div class="form-group">
@Html.LabelFor(model => model.ItemID, "ItemID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ItemID", null, "-- Select --", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ItemID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.item_qty, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@if (ViewBag.isValid == false)
{
@Html.TextBoxFor(model => model.item_qty, new { disabled = "disabled", @Value = Model.item_qty, @readonly = "readonly" })
}
else
{
@Html.EditorFor(model => model.item_qty, new { htmlAttributes = new { onchange = "OnChangeEvent(this)", min = 0, @class = "form-control", @id = "itemQTY"} })
@Html.ValidationMessageFor(model => model.item_qty, "", new { @class = "text-danger" })
}
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn"/>
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script type="text/javascript">
function OnChangeEvent(){
alert("value is changed");
var itemQty = $('#itemQTY').val();
if (itemQty < @Model.item_qty) {
btn.Attributes.Add("onclick", "clicked(event)");
}
}
</script>
@model PIC\u Program\u 1.\u 0.Models.JODetails
@使用PIC_程序_1._0.0模型
@{
ViewBag.Title=“编辑”;
PIC_程序_1_0上下文db=新PIC_程序_1_0上下文();
var currentData=db.JODetails.AsNoTracking().FirstOrDefault(j=>j.ID==Model.ID);
Component comp=db.Components.Find(Model.ComponentID);
Item i=db.Items.Find(Model.ItemID);
}
编辑
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
JODetails
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.ID)
@Html.HiddenFor(model=>model.subDetail)
@ViewBag.Error
@LabelFor(model=>model.ItemID,“ItemID”,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownList(“ItemID”,null,“--Select--”,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.ItemID,“,new{@class=“text danger”})
@LabelFor(model=>model.item_数量,htmlAttributes:new{@class=“controllabel col-md-2”})
@如果(ViewBag.isValid==false)
{
@Html.TextBoxFor(model=>model.item_数量,新的{disabled=“disabled”,@Value=model.item_数量,@readonly=“readonly”})
}
其他的
{
@EditorFor(model=>model.item_数量,new{htmlAttributes=new{onchange=“onchangevent(this)”,min=0,@class=“form control”,@id=“itemQTY”})
@Html.ValidationMessageFor(model=>model.item_qty,“,new{@class=“text danger”})
}
}
@ActionLink(“返回列表