本文介绍了Html.EditorFor设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

菜鸟问题.我有一个参数传递给创建视图.我需要使用默认值设置一个字段名称. @ Html.EditorFor(模型=>模型.Id)我需要将此输入字段设置为名称ID,并将其默认值通过操作链接传递给视图.

Rookie question.I have a parameter being passed to a create view. I need to set a field name with a default value. @Html.EditorFor(model => model.Id)I need to set this input field with name Id with a default value that is being passed to the view via an actionlink.

因此,如何将输入字段-@ Html.EditorFor(model => model.Id)设置为默认值.

So, how can this input field [email protected](model => model.Id) -- get set with a default value.

以下工作?在数字5是参数的情况下,我将其传递到文本字段中以设置默认值.

Would the following work?? Where the number 5 is a parameter I pass into the text field to set default value.

@Html.EditorFor(c => c.PropertyName, new { text = "5"; })

推荐答案

干净的方法是通过控制器传递创建的实体的新实例:

The clean way to do so is to pass a new instance of the created entity through the controller:

//GET
public ActionResult CreateNewMyEntity(string default_value)
{
    MyEntity newMyEntity = new MyEntity();
    newMyEntity._propertyValue = default_value;

    return View(newMyEntity);
}

如果您想通过ActionLink传递默认值

If you want to pass the default value through ActionLink

@Html.ActionLink("Create New", "CreateNewMyEntity", new { default_value = "5" })

这篇关于Html.EditorFor设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 11:05