Asp.net mvc 未设置MVC隐藏字段

Asp.net mvc 未设置MVC隐藏字段,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,对MVC来说相当陌生,所以我可能遗漏了一些东西 我有一个简单的视图,用于插入或更新模型 模型类: public class UserModel { public Guid UserID { get; set; } [Required(ErrorMessage="Required")] [DisplayName("Email Address")] public string EmailAddress { get; set; } [Required(Err

对MVC来说相当陌生,所以我可能遗漏了一些东西

我有一个简单的视图,用于插入或更新模型

模型类:

public class UserModel
{
    public Guid UserID { get; set; }

    [Required(ErrorMessage="Required")]
    [DisplayName("Email Address")]
    public string EmailAddress { get; set; }

    [Required(ErrorMessage = "Required")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Required(ErrorMessage = "Required")]
    [DisplayName("Security Token")]
    public string SecurityToken { get; set; }
}
假设用户没有提交此表单-我将UserID作为隐藏表单元素包含。这是我的看法

@using (Html.BeginForm("Configure", "Home"))
{
    @Html.HiddenFor(Model => Model.UserID)
    @Html.LabelFor(Model => Model.EmailAddress) 
    @Html.TextBoxFor(Model => Model.EmailAddress) @Html.ValidationMessageFor(m => m.EmailAddress)
    @Html.LabelFor(Model => Model.Password)
    @Html.EditorFor(Model => Model.Password) @Html.ValidationMessageFor(m => m.Password)
    @Html.LabelFor(Model => Model.SecurityToken)
    @Html.TextBoxFor(Model => Model.SecurityToken) @Html.ValidationMessageFor(m => m.SecurityToken)
    <p><input type="submit" id="setupSalesforce" value="Save" /></p>
}
并且模型设置了UserID。它只是不会写入隐藏元素

下面是get方法-但在保存之后不会运行它(我相信)


视图中的第一行是否声明模型?应该是这样的:

@model mynamespace.UserModel

这可能会帮助你我在这里找到的结果

将您的POST方法更改为

[HttpPost]
public ActionResult Configure(Models.SalesforceUserModel model)
{
    var businessLayerFunctions = new BL.Functions();
    bool success = businessLayerFunctions.InsertOrUpdateUser(model);
    ModelState.Remove("UserID")
    return View(model);
}

您可能需要在ModelState之后再次填充模型。删除

您是否正在更新
InsertOrUpdateUser
中的
模型
对象?在保存成功后,你也应该考虑。你能发布那个视图的get方法吗?你的代码>插入式UpDeDeUs[/Cuff] >只返回<代码>成功< /代码>,所以新的值永远不知道。您可以更改
InsertOrUpdateUser
以返回更新的
SalesforceUserModel
对象。或者,因为它是通过引用传递的,所以只需在
InsertOrUpdateUser
函数中更新它。我正在InsertOrUpdateUserAttach中更新模型。附加调试器,以便您可以在通过
InsertOrUpdateUser
方法时查看
模型。是的,我的第一行是@model Web.Models.UserModel
@model mynamespace.UserModel
[HttpPost]
public ActionResult Configure(Models.SalesforceUserModel model)
{
    var businessLayerFunctions = new BL.Functions();
    bool success = businessLayerFunctions.InsertOrUpdateUser(model);
    ModelState.Remove("UserID")
    return View(model);
}