简体   繁体   English

将静态值分配给模型中的字段

[英]Assign Static Value to Field in Model

My view has the user enter in values into a form. 我的视图使用户将值输入表单。 These values get passed to the controller, in which I use dbset to create the new row in my table. 这些值传递给控制器​​,在控制器中,我使用dbset在表中创建新行。 There is one field that serves as a flag for that row, which I don't want the user to define/ even know exists. 有一个字段用作该行的标志,我不希望用户定义/甚至不知道存在。 My question is, how can I assign data to this field and have it passed to the controller without the user having to provide input? 我的问题是,如何在用户不必提供输入的情况下将数据分配给该字段并将其传递给控制器​​? In this case, I want the ACTION_TYPE field to be assigned the value/string "Add". 在这种情况下,我希望为ACTION_TYPE字段分配值/字符串“添加”。

View 视图

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

var action = Html.HiddenFor(model => model.ACTION_TYPE.ToString());
string val = "Add";
if (action == null){
    action = MvcHtmlString.Create(val);
}

Controller 控制者

 public ActionResult Create()
    {
        return View();
    }

    //
    // POST: /Table8/Create

    [HttpPost]
    public ActionResult Create(HOLIDAY_DATE_TABLE holiday_date_table, tbl8_update_transactions tbl8_update_transaction)
    {
        if (ModelState.IsValid)
        {
            db.HOLIDAY_DATE_TABLE.Add(holiday_date_table);
            db.SaveChanges();
            db.tbl8_update_transactions.Add(tbl8_update_transaction);
            db.SaveChanges();
            return RedirectToAction("../Billing/HolidayDateTable");
        }
        return View(holiday_date_table);
    }

Tables 桌子

 CREATE TABLE HOLIDAY_DATE_TABLE
 (
 HID INT IDENTITY PRIMARY KEY,
 TABLE_NUMBER nchar(2) NOT NULL,
 HOLIDAY_DATE nchar(8) NOT NULL,
 FIELD_DESCRIPTION nVARchar(43) NULL,
 ACTION_TYPE nchar(6) NULL
 );


 CREATE TABLE tbl8_update_transactions
 (
 TID INT IDENTITY PRIMARY KEY,
 TABLE_NUMBER nchar(2) NOT NULL,
 HOLIDAY_DATE nchar(8) NOT NULL,
 FIELD_DESCRIPTION nVARchar(43) NULL,
 ACTION_TYPE nchar(6) NULL,
 HID int,
 FOREIGN KEY (HID) REFERENCES HOLIDAY_DATE_TABLE (HID) ON DELETE CASCADE
 );

If the user isn't inputting this data then it doesn't belong in the form being submitted by the user. 如果用户未输入此数据,则该数据不属于用户提交的表单。 Remove it from the view entirely and set it in the server-side code. 将其从视图中完全删除,并在服务器端代码中进行设置。 One simple way to do this could be in the controller action: 一种简单的方法是在控制器动作中:

// completely guessing on types/models here, but you should get the idea...
holiday_date_table.ActionType = "Add";
db.HOLIDAY_DATE_TABLE.Add(holiday_date_table);
db.SaveChanges();

Or you can even set it by default in the model itself (which is ideally where logic like this goes), the design of which is going to depend very heavily on your model structure. 或者,您甚至可以默认在模型本身中设置它(这是理想的逻辑位置),其设计将在很大程度上取决于您的模型结构。 A default value might be set in a constructor, or late-bound in a property, or perhaps by separating the concepts of "adding" vs. "editing" into their own models. 可以在构造函数中设置默认值,也可以在属性中后期设置默认值,或者可以通过将“添加”与“编辑”的概念分成各自的模型来进行设置。

Either way, it shouldn't be in the view. 无论哪种方式,都不应该在视图中显示。 Even as a hidden field, any user can change that value whenever they like. 即使是隐藏字段,任何用户都可以随时更改该值。 The only way for your code to genuinely control the value is to keep it server-side. 您的代码真正控制值的唯一方法是将其保留在服务器端。


As an aside, any time you declare a server-side variable in a view then you're most likely doing something wrong. 顺便说一句,每当您在视图中声明服务器端变量时,您很可能做错了什么。 Views shouldn't have business logic, they just bind a UI to a model. 视图不应该具有业务逻辑,它们只是将UI绑定到模型。 The logic really belongs in the model. 逻辑确实属于模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM