Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将视图中的值作为输入值传递给ASP.NET MVC中的表单_C#_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 将视图中的值作为输入值传递给ASP.NET MVC中的表单

C# 将视图中的值作为输入值传递给ASP.NET MVC中的表单,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我有一个视图称为子操作: @Html.Action("RenderPostMessage", "JobSurface") 控制器如下所示: public ActionResult RenderPostMessage() { PostMessageViewModel postMessageViewModel = new PostMessageViewModel(); return PartialView("PostMessage", postMessageViewModel);

我有一个视图称为子操作:

@Html.Action("RenderPostMessage", "JobSurface")
控制器如下所示:

public ActionResult RenderPostMessage()
{
    PostMessageViewModel postMessageViewModel = new PostMessageViewModel();
    return PartialView("PostMessage", postMessageViewModel);
}
@model PostMessageViewModel

@{
    Html.EnableClientValidation(true);
    Html.EnableUnobtrusiveJavaScript(true);
}
@using (Html.BeginUmbracoForm<JobSurfaceController>("HandlePostMessage", new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)       

    <p>
        @Html.EditorFor(model => model.Message)
        @Html.ValidationMessageFor(model => model.Message)
    </p>

    <p>
        @Html.LabelFor(model => model.File)
        @Html.TextBoxFor(x => x.File, new { type = "file" })
    </p>
    <p><button class="button">Post Message</button></p>
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult HandlePostMessage(PostMessageViewModel model)
{
    // Some logic
}
此调用的部分内容如下所示:

public ActionResult RenderPostMessage()
{
    PostMessageViewModel postMessageViewModel = new PostMessageViewModel();
    return PartialView("PostMessage", postMessageViewModel);
}
@model PostMessageViewModel

@{
    Html.EnableClientValidation(true);
    Html.EnableUnobtrusiveJavaScript(true);
}
@using (Html.BeginUmbracoForm<JobSurfaceController>("HandlePostMessage", new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)       

    <p>
        @Html.EditorFor(model => model.Message)
        @Html.ValidationMessageFor(model => model.Message)
    </p>

    <p>
        @Html.LabelFor(model => model.File)
        @Html.TextBoxFor(x => x.File, new { type = "file" })
    </p>
    <p><button class="button">Post Message</button></p>
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult HandlePostMessage(PostMessageViewModel model)
{
    // Some logic
}
我在视图中有一堆变量,我需要以某种方式传递到表单中(可能作为隐藏的输入字段?),但尽管我知道如何在部分上创建隐藏的输入,但我不知道如何用视图中的值填充它们

有人能建议如何将值传递给控制器吗


非常感谢。

@Html.Action
有一个参数“routeValues”,它是一个匿名对象。您可以在那里传递值。因此……从观点到行动:

@Html.Action("RenderPostMessage", routeValues:new{SurfaceType = "JobSurface", OtherValue = "Something", NewValue = "Something else"});
操作接受这些路由值作为方法参数:

    public ActionResult RenderPostMessage(string surfaceType, string otherValue, string newValue)
    {
        var viewModel = new PostMessageViewModel();
viewModel.SurfaceType = surfaceType;
viewModel.OtherValue = otherValue;
viewModel.NewValue = newValue;
        return PartialView("PostMessage", viewModel);
    }
完成了

视图中有一堆变量,我需要以某种方式传递它们 到表单(可能作为隐藏的输入字段?)

很简单,如果要使用值渲染隐藏的输入字段,请将其添加到视图中的
ViewBag
对象中

例如,如果要将变量的内容添加到表单中,请在视图中执行以下操作:

ViewBag.Foo=“一些值”

然后在cshtml文件中添加隐藏字段:

@Html.Hidden(“Foo”)

这样,您将在表单post中收到值

编辑:这就是代码的外观。

public ActionResult RenderPostMessage()
{
    PostMessageViewModel postMessageViewModel = new PostMessageViewModel();

    // here you set as many values as you want to receive in the form post.
    ViewBag.SomeField = "Some Value";

    return PartialView("PostMessage", postMessageViewModel);
}
查看

@model PostMessageViewModel

@{
    Html.EnableClientValidation(true);
    Html.EnableUnobtrusiveJavaScript(true);
}
@using (Html.BeginUmbracoForm<JobSurfaceController>("HandlePostMessage", new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)       

    @Html.Hidden("SomeField")

    <p>
        @Html.EditorFor(model => model.Message)
        @Html.ValidationMessageFor(model => model.Message)
    </p>

    <p>
        @Html.LabelFor(model => model.File)
        @Html.TextBoxFor(x => x.File, new { type = "file" })
    </p>
    <p><button class="button">Post Message</button></p>
}
@model PostMessageViewModel
@{
Html.EnableClientValidation(true);
EnableUnobtrusiveJavaScript(true);
}
@使用(Html.BeginUmbracoForm(“HandlePostMessage”,new{enctype=“multipart/form data”}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.Hidden(“SomeField”)

@EditorFor(model=>model.Message)
@Html.ValidationMessageFor(model=>model.Message)

@LabelFor(model=>model.File) @Html.TextBoxFor(x=>x.File,新的{type=“File”})

留言

}
@Html.Action(“RenderPostMessage”…
但是您的ActionResult签名是HandlePostMessage(这只是一个打字错误吗?).既然你有一个视图模型,为什么不直接添加
test
作为视图模型的公共属性呢?谢谢@Stephen。我是MVC的一个新手,所以还在努力解决这个问题。我想可能会更复杂一些,所以我会更新我的原始帖子,提供更多细节。你要求“填充[变量]使用“视图中的值”,但您接受了从控制器填充变量的答案。为什么?另外,既然您有一个“PostMessageViewModel”,为什么您要冒险使用ViewBag进入动态区域…只需将它们设置为PostMessageViewModel上的属性。谢谢。我现在看到使用
name=“Foo”创建了一个隐藏输入
属性,但它不包含值(当我在浏览器开发工具中查看时)。应该吗?一旦我确定了,我就可以继续尝试获取代码中的值并将其投入使用。@Dan您是否在
视图包中设置了值
。您需要在隐藏字段中设置所需的值。