我的ASP.NET MVC 4应用程序我有一个与国家/地区下拉菜单的联系表单.对于该菜单,我使用Html.DropDownListFor帮助方法.但是如果没有选择country,我会遇到异常:"没有类型为'IEnumerable'的ViewData项具有键'Country.Name'." 这是我的控制器:
ContactViewModel contactViewModel = new ContactViewModel();
contactViewModel.Countries = DbUnitOfWork.CountriesRepository.GetAll()
.Select(c => c.Name)
.AsEnumerable()
.Select(c => new SelectListItem
{
Value = c.ToString(),
Text = c.ToString()
});
Run Code Online (Sandbox Code Playgroud)
这是在视图中:
@Html.DropDownListFor(m => m.Country.Name,
Model.Countries,
Resources.SelectCountry,
dropdown)
Run Code Online (Sandbox Code Playgroud)
如您所见,我没有使用ViewData.如何防止这种异常?我的模型属性[必需]但没有显示错误消息.
更新
这是我的视图模型中的国家/地区字段:
public IEnumerable<SelectListItem> Countries { get; set; }
[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "CountryErrorMessage")]
[Display(ResourceType = typeof(Resource), Name = "Country")]
public CountryModel Country { get; set; }
Run Code Online (Sandbox Code Playgroud)
这是在视图中
@model MyApp.ViewModels.ContactViewModel
Run Code Online (Sandbox Code Playgroud) 我有一个文件夹.pdf
.在大多数文件的名称中,我想用另一个字符串替换特定的字符串.
这就是我写的.
private void btnGetFiles_Click(object sender, EventArgs e)
{
string dir = tbGetFIles.Text;
List<string> FileNames = new List<string>();
DirectoryInfo DirInfo = new DirectoryInfo(dir);
foreach (FileInfo File in DirInfo.GetFiles())
{
FileNames.Add(File.Name);
}
lbFileNames.DataSource = FileNames;
}
Run Code Online (Sandbox Code Playgroud)
在这里,我提取列表框中的所有文件名.
private void btnReplace_Click(object sender, EventArgs e)
{
string strReplace = tbReplace.Text; // The existing string
string strWith = tbWith.Text; // The new string
string dir = tbGetFIles.Text;
DirectoryInfo DirInfo = new DirectoryInfo(dir);
FileInfo[] names = DirInfo.GetFiles();
foreach (FileInfo f in names)
{ …
Run Code Online (Sandbox Code Playgroud) 我有一个复合主键表由两列组成,该表也有一个唯一键.另一个表中的唯一键被映射为外键.如何用Hibernate实现这个映射?我为复合主键创建一个@Embedable类,在实体中我使用注释@EmbeddedId.在第二个表中,我声明了一个字段:
@ManyToOne
@JoinColumn(name = "user_code", nullable = false)
public User userCode;
Run Code Online (Sandbox Code Playgroud)
这是第一个表中的唯一键,但我得到一个例外:从com.users.maintenance.Code引用com.users.maintenance.User的外键具有错误的列数.应该是2.
编辑 这是我的用户类:
@Entity
@Table(name = "Users")
public class User {
@EmbeddedId
public UserPk userId;
@column(name = "code")
public int code;
public UserPk getUserId() {
return this.userId;
}
public void setUserId(UserPk userId) {
this.userId = userId;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
@Embeddable
private class UserPk implements Seriazible {
@Column(name = "user_name")
public String userName;
@Column(name …
Run Code Online (Sandbox Code Playgroud) 我正在尝试更新Access数据库中的表.但是当我调试时,我得到了异常:"无效的UPDATE语句".我的陈述有什么问题:
string query = @"UPDATE Phone SET Name = ?, Number = ? WHERE ID = ?";
Run Code Online (Sandbox Code Playgroud) 我正在开发一个Fortran应用程序,用于数值求解边界值问题的二阶ODE类型:-y''+ q(x)*y = r(x).在这个应用程序中,我使用高斯消除算法来求解线性方程组并将解决方案写入文件中.但对于解决方案向量,我收到了NaN.为什么会这样?这是一些代码.
subroutine gaussian_solve(s, c, error)
double precision, dimension(:,:), intent(in out) :: s
double precision, dimension(:), intent(in out) :: c
integer :: error
if(error == 0) then
call back_substitution(s, c)
end if
end subroutine gaussian_solve
!=========================================================================================
!================= Subroutine gaussian_ellimination ===============================
subroutine gaussion_ellimination(s, c, error)
double precision, dimension(:,:), intent(in out) :: s
double precision, dimension(:), intent(in out) :: c
integer, intent(out) :: error
real, dimension(size(s, 1)) :: temp_array
integer, dimension(1) :: ksave
integer :: i, j, k, n
real …
Run Code Online (Sandbox Code Playgroud)