表单不提交文本框的新值 [英] form not submitting text box's new value

查看:202
本文介绍了表单不提交文本框的新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,其文本框是通过循环生成的,并包含其默认值。但是提交后,我得到了它的默认值,而不是刚刚更新的值。



以下是 cshtml 代码

 < fieldset> 
< legend>模块< small>编辑< / small>< / legend>
@using(Html.BeginForm(Edit,Module))
{
@ Html.ValidationSummary(true)
@ Html.HiddenFor(m => m .Id)
for(var i = 0; i< Model.Properties.Count(); i ++)
{
@ Html.HiddenFor(model => Model.HiddenProperties [i ] .Value)
< label class =label> @ Model.Properties [i] .Name< / label>
< div class =input-block-level> @ Html.TextBoxFor(model => Model.Properties [i] .Value,new {@value = Model.Properties [i] .Value} )< / DIV>
}

< div class =form-actions>
< button type =submitclass =btn btn-primaryid =Submit>保存更改< / button>
@ Html.ActionLink(Cancel,ModuleList,null,new {@class =btn})
< / div>

}
< / fieldset>

关于我在做什么错误的想法?
我使用的是hidden,这样我就可以检查用户是否改变了以前的值。



Edit1:
我试过了

$ $ $ $ $ $ $ $ $ $ $ b $ $

 新{@Value = Model。 Properties [i] .Value} 

仍然无效。

Edit2:
刚刚意识到它默认显示当前值,所以我删除了textboxfor中的新部分。仍然不能获得控制器后处理方法中的更新值。因此,现在我有了

 < div class =input-block-level> @ Html.TextBoxFor(model => ; Model.Properties [i] .Value)< / div> 

Edit3:这里是图片使事情更清晰





现在在下面的图片中,我尝试将 999888 值的 MicrophoneArrayRackModuleId 的值更改为 99988877 只需在当前值的末尾键入77,然后单击保存更改按钮



后点击>

保存更改按钮我将转发到 HttpPost 方法,并在检查模块参数我看到的图片中显示的值的方法。我以为它是 99988877 ,但它显示 999888





Edit4:
在运行开发人员工具从铬,我检查了网络 - > httppost方法 - > Headers-> formdata,这就是它显示

  Id: 50 
HiddenProperties [0]。值:999888
属性[0]。值:99988877
HiddenProperties [1]。值:000-00000-000
属性[1]。 Value:000-00000-000
HiddenProperties [2] .Value:1a2b3c
Properties [2] .Value:1a2b3c

这是我期望在控制器的后期方法中的值,但这种情况没有发生。



Edit5:
由Darin提到我在视图中添加了名称字段,它现在看起来像

  for(var i = 0; i< Model.Properties.Count(); i ++)
{
@ Html.HiddenFor(model =&g t; model.HiddenProperties [i] .Name)
@ Html.HiddenFor(model => model.HiddenProperties [i] .Value)
< label class =label> @Model。特性[Ⅰ]请将.Name< /标签>
< div class =input-block-level> @ Html.TextBoxFor(model => model.Properties [i] .Value)< / div>
}

仍然无效

模型

  public class PropertyViewModel 
{
public string Name {get;组; }
public string Value {get;组; }
}

public class EditModule
{
public long Id {get;组; }
public List< PropertyViewModel>属性{get;组; }
public List< PropertyViewModel> HiddenProperties
{
get {return Properties; }
set {Properties = value; }




控制器 p>

  [HttpGet] 
public ActionResult编辑(long id)
{
var module = _repository。 GetModuleProperties(ID);
return View(module);

$ b [HttpPost]
public ActionResult Edit(EditModule模块)
{
if(ModelState.IsValid)
{
_repository.SaveModuleEdits(module);
信息(模块已成功编辑!);
返回RedirectToAction(ModuleList,Module,new {area =Hardware});
}
错误(编辑失败,如果问题仍然存在,请联系Merijn!);
返回RedirectToAction(ModuleList,Module,new {area =Hardware});


存储库

  public EditModule GetModuleProperties(long id)
{
var properties = new EditModule
{
Id = id,
Properties = _dbSis.Modules.Where(t => t.Id == id)
.SelectMany(m => m.PropertyConfiguration.PropertyInstances.Select(i => new PropertyViewModel
{
Name = i.Property.Name,
Value = i.Value
}))。ToList()

};
返回属性;
}


解决方案

您从未发送过Name属性,这就是为什么它总是NULL。请确保您将其作为隐藏字段包含:

  @ Html.HiddenFor(model => model.HiddenProperties [i]。名称)

也不要捕捉模型在关闭。使用模型变量来代替表达式:

  @Html。 HiddenFor(model => model.HiddenProperties [i] .Value)

你绝对不会需要设置 Value 属性,这就是Html助手的用途:

  @ Html.TextBoxFor(model => model.Properties [i] .Value)


I have a form, whose textboxes are generated via loop and contains its default value. But upon submission, I get its default value and not the value that was just updated.

Here is the cshtml code

<fieldset>
    <legend>Module <small>Edit</small></legend>
     @using (Html.BeginForm("Edit", "Module"))
    {
        @Html.ValidationSummary(true)
        @Html.HiddenFor(m=>m.Id)
        for(var i = 0; i < Model.Properties.Count(); i++)
        {
             @Html.HiddenFor(model=>Model.HiddenProperties[i].Value)
            <label class="label">@Model.Properties[i].Name</label>
            <div class="input-block-level">@Html.TextBoxFor(model => Model.Properties[i].Value, new { @value = Model.Properties[i].Value })</div>
        }

         <div class="form-actions">
        <button type="submit" class="btn btn-primary" id="Submit">Save changes</button>
        @Html.ActionLink("Cancel", "ModuleList", null, new { @class = "btn " })
    </div>

    }
</fieldset>

Any idea on what mistake I'm doing? I'm using hiddenfor so that I can check if user has changed the previous value.

Edit1: I tried

new { Value = Model.Properties[i].Value } 

and

new { @Value = Model.Properties[i].Value }

Still didn't work.

Edit2: just realised it shows current value by default, so i removed the new part in textboxfor. Still don't get the updated value in post method of the controller. So now i have

<div class="input-block-level">@Html.TextBoxFor(model => Model.Properties[i].Value)</div>

Edit3: here is the image to make things more clear

now in following image I try to change the value of MicrophoneArrayRackModuleId's value form 999888 to 99988877 just by typing 77 at the end of current value and i click save changes button

after clicking save changes button i get forwarded to HttpPost method, and upon inspecting the module parameter in the method I see the value that is shown in picture. I was expecting it to be 99988877 but it shows 999888

Edit4: On running developers tool from chrome, I checked network->httppost method->Headers->formdata and this is what it shows

Id:50
HiddenProperties[0].Value:999888
Properties[0].Value:99988877
HiddenProperties[1].Value:000-00000-000
Properties[1].Value:000-00000-000
HiddenProperties[2].Value:1a2b3c
Properties[2].Value:1a2b3c

which is the value i'm expecting in post method of controller but thats not happening.

Edit5: as mentioned by Darin I added name field in view and it now looks like

        for(var i = 0; i < Model.Properties.Count(); i++)
        {
            @Html.HiddenFor(model=>model.HiddenProperties[i].Name)
             @Html.HiddenFor(model=>model.HiddenProperties[i].Value)
            <label class="label">@Model.Properties[i].Name</label>
            <div class="input-block-level">@Html.TextBoxFor(model => model.Properties[i].Value)</div>
        }

still it doesn't work

Edit6: Model

  public class PropertyViewModel
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }

public class EditModule
{
    public long Id { get; set; }
    public List<PropertyViewModel> Properties { get; set; }
    public List<PropertyViewModel> HiddenProperties
    {
        get { return Properties; }
        set { Properties = value; }
    } 
}

Controller

[HttpGet]
public ActionResult Edit(long id)
{
    var module = _repository.GetModuleProperties(id);
    return View(module);
}

    [HttpPost]
    public ActionResult Edit(EditModule module)
    {
        if (ModelState.IsValid)
        { 
             _repository.SaveModuleEdits(module); 
            Information("Module was successfully edited!");
            return RedirectToAction("ModuleList", "Module", new {area = "Hardware"});
        }
        Error("Edit was unsuccessful, if the problem persists please contact Merijn!");
        return RedirectToAction("ModuleList", "Module", new { area = "Hardware" });

    }

Repository

public EditModule GetModuleProperties(long id)
{
    var properties = new EditModule
        {
            Id = id,
            Properties =_dbSis.Modules.Where(t => t.Id == id)
                                      .SelectMany(m => m.PropertyConfiguration.PropertyInstances.Select(i => new PropertyViewModel
                                       {
                                           Name = i.Property.Name,
                                           Value = i.Value
                                       })).ToList()

        };
    return properties;
}

解决方案

You never sent the Name property, that's why it is always NULL. Make sure you include it as a hidden field:

@Html.HiddenFor(model => model.HiddenProperties[i].Name)

Also do not capture the Model in a closure. Use the model variable instead when writing your expressions:

@Html.HiddenFor(model => model.HiddenProperties[i].Value)

Also you absolutely don't need to be setting the Value attribute, that is what the Html helper is for:

@Html.TextBoxFor(model => model.Properties[i].Value)

这篇关于表单不提交文本框的新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆