我在表单视图中定义了以下隐藏的输入字段,
形式:
@model Mysite.Models.FileUpload
@using Mysite.Models
@{
var FileUploadData = ViewData["FileUploadData"] as FileUpload;
}
@using (Html.BeginForm("LoadData",
"LoadAndAnalyzeData",
FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<div class="div-root" id="serverslist">
@Html.LabelFor(m => m.SelectedServer)
@Html.DropDownListFor(m =>m.SelectedServer, FileUploadData.ServersList)
</div>
<div class="div-root">
<label for="files">Choose File 1: </label>
<input type="file" name="file1" id="file1" />
</div>
<div class="div-root">
<input type="submit" id="upload" name ="FileUpload" value="Upload Files For Analysis" />
</div>
}
@using (Html.BeginForm("AnalyzeLoadedData", "LoadAndAnalyzeData"))
{
<input type="hidden" name="servername" id="servername" value='@ViewBag.servername' />
@ViewBag.servername
<input type="submit" id="analysis" name ="AnalyzeLoadedData" value="Analyze Loaded Data" />
}
我有一个模型,我在下面定义了“servername”,
public string servername { get; set; }
当我单击提交时,函数AnalyzeLoadedData
未按预期获取服务器名称。但该页面在页面上显示服务器名称作为@ViewBag.servername
的一部分。
“servername当前上下文中不存在名称'servername'”
请指导我在这里缺少什么。
答案 0 :(得分:1)
以下是如何使用您在MVC中创建的名为servername
的属性并获取结果的示例。我不确定您是否在模型中定义了servername
,或者您只是拥有随机属性。
您将servername作为属性的类可能如下所示:
public class Server
{
public string servername { get; set; }
}
您的控制器可能如下所示:
public class LoadAndAnalyzeDataController : Controller
{
public ActionResult LoadAndAnalyzeData()
{
//It is not clear why you have called your action and controller
//the same thing. Do you know what your action and controller are?
//You could use a view bag like this if you really want but it
//has nothing to do with the property you mentioned in your
//question...
ViewBag.servername = "......";
//Or you could make an instance of the Server class and set your
//servername property to a value like this:
Server myServer = new Server(){ servername = "...." };
//After making an instance make sure to pass it as a parameter
//to your view like this:
return View(myServer);
}
[HttpPost]
public ActionResult LoadAndAnalyzeData(string servername)
{
//do something with servername
return View();
}
}
最后你的观点看起来就像你发布的那样:
//Dont forget to refer to your model at the top of your page if you would
//like to use the data you pass through your controller.
@model Server
@using (Html.BeginForm("AnalyzeLoadedData", "LoadAndAnalyzeData"))
{
<input type="hidden" name="servername" id="servername" value='@Model.servername' />
//you can also use the ViewBag if you have populated it with data like
//you had:
@ViewBag.servername
//It is very wierd to give a value and name to your submit button like
//you had it here:
<input type="submit" id="analysis" name ="AnalyzeLoadedData" value="Analyze Loaded Data" />
//But anyway if you put a break point in the [HttpPost] action in the
//controller which you refer to in this begin form you will see your
//value for 'servername' coming through as a parameter.
}
答案 1 :(得分:0)
好的,您刚刚更新了帖子。您应该放弃使用ViewBag并使用剃须刀助手。改为:
@ Html.HiddenFor(x =&gt; x.servername)