【发布时间】:2017-11-30 00:37:00
【问题描述】:
Grrr 我对此有很多问题。希望有人可以在这里提供帮助。在您回答之前,请记住我已经搜索过并且已经找到了很多关于此的主题。
目前,我正在尝试使用典型的 POST 请求填充 HTML 表单(第一次这样做,我通常是一个非常优秀的程序员,但是 ASP .NET Web API 中的这个 Web 服务东西让我很生气,因为奇怪的特质。)我的下拉列表应包含以下枚举作为可供选择的值:
public enum FuelTypes
{
Gasoline = 0,
Diesel = 1
};
是的,我确保在控制器中调用 View 时传入我的模型实例。
在“调试”模式下不断中断的行在此处的 HTML 中(您可以看到我在下面为清楚起见将其注释掉了。):
@Html.DropDownListFor(model => model.FuelId, Model.FuelTypesList, "--Fuel Type--")
所以这次肯定有其他问题。请看下面我分别用 C# 和 HTML 编写的相关代码:
附:任何其他改进我已经拥有的东西的提示都会很棒!我对此很陌生。但我需要能够获取这些下拉值来填充和显示。然后我必须从我的 HttpWrapper 对象中创建相关对象,其中包含所有正在发布的数据。
提前致谢!
我的模特:
public class FuelModel
{
public FuelModel()
{
FuelTypesList = new List<SelectListItem>();
}
[Display(Name = "FuelType")]
public int FuelId
{
get; set;
}
public IEnumerable<SelectListItem> FuelTypesList
{
get; set;
}
}
我的控制者:
public class ActionController : Controller
{
// POST: Action
[HttpPost]
[ActionName("FuelType")]
public ActionResult Index(FuelModel Model)
{
Model = new FuelModel();
// This is where the "FuelType" Enum is referenced to access values
later from the HTML form.
IEnumerable<FuelTypes> FuelType =
Enum.GetValues(typeof(FuelTypes)).Cast<FuelTypes>();
Model.FuelTypesList =
from action in FuelType
select new SelectListItem
{
Text = action.ToString(),
Value = ((int)action).ToString()
};
return View(Model);
}
}
我的观点(包含 HTML/JavaScript):
<div id="body">
<ul id="caradvertisements"></ul>
</div>
<form id="saveCarAdvertisementForm" method="POST">
<h3>Create A New Car Advertisement</h3>
<p>
<label for="New">New or Used:</label>
<div align="left">
<select name="mydropdown">
<option value="New">New</option>
<option value="Used">Used</option>
</select>
</div>
</p>
<p>
<label for="Id">Car Id:</label>
<input type="text" name="Id" />
</p>
<p>
<label for="Title">Car Title:</label>
<input type="text" name="Title" />
</p>
<p>
@model AutoWebService.Models.FuelModel
@{
ViewBag.Title = "Index";
}
@Html.LabelFor(model => model.FuelId)
@Html.DropDownListFor(model => model.FuelId, Model.FuelTypesList)
@Html.DropDownListFor(model => model.FuelId, Model.FuelTypesList, "--Fuel Type--")
<div align="left">
<label for="Fuel">Car Fuel Type:</label>
<select name="mydropdown">
<option value="Gasoline">Gasoline</option>
<option value="Diesel">Diesel</option>
</select>
</div>
</p>
<p>
<label for="Price">Car Price:</label>
<input type="text" name="Price" />
</p>
<input type="button" id="saveCarAdvertisement" value="Save" />
</form>
@section scripts{
<script type="text/javascript">
$(function()
{
$.getJSON('/api/caradvertisement', function(caradvertisementsJsonPayload)
{
$(caradvertisementsJsonPayload).each(function (i, item)
{
$('#caradvertisements').append('<li>' + item.Name + '</li>');
});
});
});
$('#saveCarAdvertisement').click(function ()
{
$.post("api/caradvertisement",
$("#saveCarAdvertisementForm").serialize(),
function (value)
{
$('#caradvertisements').append('<li>' + value.Name + '</li>');
},
"json"
);
});
</script>
}
【问题讨论】:
-
你能详细解释一下吗,我检查了你的代码。最初没有呈现任何内容,我删除了 [HttpPost] 属性,然后呈现了表单。
-
您好,当我在 Visual Studio 2015 中运行所有内容时出现以下异常: System.NullReferenceException 未被用户代码处理 HResult=-2147467261 消息=未将对象引用设置为对象的实例。 Source=App_Web_bkzznufe StackTrace: at ASP._Page_Views_Home_Index_cshtml.Execute() in c:\users\brad\documents\visual studio 2015\Projects\AutoScout24\AutoWebService\Views\Home\Index.cshtml:line 33
-
能不能分享一下code code的解决方案。
-
是的,当然。我怎样才能把它寄给你?
-
通过在此处发布代码文件
标签: c# html asp.net rest asp.net-mvc-4