如何在从一个控制器调用时使文本框成为只读,而在从另一个控制器调用时不是只读的?

时间:2017-06-16 13:59:36

标签: c# asp.net-mvc razor

我目前有一个用于创建和编辑模型的视图。对于编辑操作,页面上的字段将自动填充模型中的数据,并可以进行编辑然后保存。我喜欢我的控制器中的一个类似的操作,用一些" View"的数据填充字段。页。我读到我可以将htmlAttribute @readonly="readonly"添加到我的文本框中,但是只有在从此操作调用视图时,我才能将此属性附加到文本框(或更简单)?我不确定这里的代码是什么,更多是一个概念性的问题,但这里有一些代码以防万一:

我想要用于所有三个功能(创建,编辑和查看)的视图称为" Create.cshtml"并且充满了:

@Html.LabelFor(model => model.Project.SubmissionNumber)
@Html.EditorFor(model => model.Project.SubmissionNumber)
@Html.ValidationMessageFor(model => model.Project.SubmissionNumber, "", new { @class = "text-danger" })

以及一些下拉菜单:

@Html.LabelFor(model => model.Project.Status)
@Html.DropDownListFor(model => model.Project.Status,
                      Model.Statuses,
                      "--")
@Html.ValidationMessageFor(model => model.Project.Status, "", new { @class = "text-danger" })

我不会包含整个视图,因为它非常重复。像这样想象一堆文本框和下拉菜单。

我的创建动作:

public ActionResult Create()
{
    var model = new ProjectClearanceApp.Models.ProjectViewModel();
    var statuses = GetAllStatuses();
    var divisions = GetAllDivisions();
    var branches = GetAllBranches();
    var project_types = GetAllProjectTypes();
    var states = GetAllStates();

    model.Statuses = GetSelectListItems(statuses);
    model.Divisions = GetSelectListItems(divisions);
    model.Branches = GetSelectListItems(branches);
    model.ProjectTypes = GetSelectListItems(project_types);
    model.States = GetSelectListItems(states);

    return View(model);
}

// POST: Projects/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ProjectName,ProjectDescription,DateReceived,EffectiveDate,ExpirationDate,GeneralContractor,ProjectTerm,ProjectType,SubmissionNumber,PolicyNumber,Status,Underwriter,Division,BrokerCity,TAName,Branch,FirstNamedInsuredAddress,FirstNamedInsured,ProjectAddress")] Project project)
{
    if (ModelState.IsValid)
    {
        if (project.ProjectID <= 0)
        {
            db.Projects.Add(project);
        }
        else
        {
            db.Entry(project).State = EntityState.Modified;
        }
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    var model = new ProjectViewModel();
    model.Project = db.Projects.Find(project.ProjectID);

    var states = GetAllStates();
    var branches = GetAllBranches();
    var divisions = GetAllDivisions();
    var project_types = GetAllProjectTypes();
    var statuses = GetAllStatuses();

    model.States = GetSelectListItems(states);
    model.Branches = GetSelectListItems(branches);
    model.Divisions = GetSelectListItems(divisions);
    model.ProjectTypes = GetSelectListItems(project_types);
    model.Statuses = GetSelectListItems(statuses);

    return View(model);
}

所有这些GetAll_____s()GetSelectListItems(_____)都会填充填充下拉菜单的枚举。

我的编辑动作(令人震惊的相似):

public ActionResult Edit(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    var model = new ProjectViewModel();
    model.Project = db.Projects.Find(id);

    if (model.Project == null)
    {
        return HttpNotFound();
    }

    var states = GetAllStates();
    var branches = GetAllBranches();
    var divisions = GetAllDivisions();
    var project_types = GetAllProjectTypes();
    var statuses = GetAllStatuses();

    model.States = GetSelectListItems(states);
    model.Branches = GetSelectListItems(branches);
    model.Divisions = GetSelectListItems(divisions);
    model.ProjectTypes = GetSelectListItems(project_types);
    model.Statuses = GetSelectListItems(statuses);

    return View("Create", model);
}

// POST: Projects/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ProjectID,ProjectName,ProjectDescription,DateReceived,EffectiveDate,ExpirationDate,GeneralContractor,ProjectTerm,ProjectType,SubmissionNumber,PolicyNumber,Status,Underwriter,Division,BrokerCity,TAName,Branch,FirstNamedInsuredAddress,FirstNamedInsured,ProjectAddress")] Project project)
{
    if (ModelState.IsValid)
    {
        if (project.ProjectID <= 0)
        {
            db.Projects.Add(project);
        } else
        {
            db.Entry(project).State = EntityState.Modified;
        }
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    var model = new ProjectViewModel();
    model.Project = db.Projects.Find(project.ProjectID);

    var states = GetAllStates();
    var branches = GetAllBranches();
    var divisions = GetAllDivisions();
    var project_types = GetAllProjectTypes();
    var statuses = GetAllStatuses();

    model.States = GetSelectListItems(states);
    model.Branches = GetSelectListItems(branches);
    model.Divisions = GetSelectListItems(divisions);
    model.ProjectTypes = GetSelectListItems(project_types);
    model.Statuses = GetSelectListItems(statuses);

    return View("Create", model);
}

我假设&#34; View&#34;我想要只读的动作应该类似于&#34; GET&#34;编辑/创建操作,但我不知道需要添加什么才能使这些文本框成为只读。

1 个答案:

答案 0 :(得分:0)

我为实现我真正想要的而做的是我在我的ViewModel中添加了bool ReadOnly并将所有文本框和下拉列表包装在if / else语句中,如下所示:

@if (Model.ReadOnly)
{
    @Html.EditorFor(model => model.Project.SubmissionNumber, new { htmlAttributes = new { @readonly = "readonly" } })
}
else
{
    @Html.EditorFor(model => model.Project.SubmissionNumber)
}

和此:

@if (Model.ReadOnly)
{
    @Html.DropDownListFor(model => model.Project.Status,
                          Model.Statuses,
                          "--",
                          new { @disabled = "disabled" })
}
else
{
    @Html.DropDownListFor(model => model.Project.Status,
                          Model.Statuses,
                          "--")
}

现在我可以为所有三种方法重用完全相同的视图,而无需复制粘贴代码。