IdoS | Computer Science
IdoS | Computer Science
CC#
Created by IdoS | Computer Science on 7/26/2023 in #help
❔ @Html.DropDownListFor results in null for selected item
Hi for the following command: @Html.DropDownListFor(m=>m.Name, new SelectList(Model.Names)) I get null after submitting form for Name. why is that?
public class IndexModel : PageModel
{
private readonly WebApplication1.Data.ServerContext _context;

public IndexModel(WebApplication1.Data.ServerContext context)
{
_context = context;
Names = _context.TblCountries.Select(x => x.CountryName).OrderBy(c => c).ToList();
}
[BindProperty]
public NewPlayerRegister NewPlayerRegister { get; set; } = new NewPlayerRegister();

public List<string> Names { get; set; } = default!;

public string Name { get; set; } = default!;

public IList<TblCountries> TblCountries { get; set; } = default!;
public void OnPost()
{
if (string.IsNullOrEmpty(this.Name))
{
ModelState.AddModelError(string.Empty, "Please select a country.");
Page();
return;
}
NewPlayerRegister.SetCountry(this.Name);
if (ModelState.IsValid)
{
// Process the form data and perform other necessary actions
string res = NewPlayerRegister.Name + "<br>" + NewPlayerRegister.Id + "<br>" + NewPlayerRegister.Phone + "<br>" + NewPlayerRegister.CountrySelection;


// Save data to the database using _context
// _context.NewPlayerRegisters.Add(NewPlayerRegister);
_context.SaveChangesAsync();

// or return any appropriate action result
this.Response.WriteAsync("<p>" + res + "</p>");
}
else
{
// Return to the page to display form validation error messages
Page();
}
}
}
public class IndexModel : PageModel
{
private readonly WebApplication1.Data.ServerContext _context;

public IndexModel(WebApplication1.Data.ServerContext context)
{
_context = context;
Names = _context.TblCountries.Select(x => x.CountryName).OrderBy(c => c).ToList();
}
[BindProperty]
public NewPlayerRegister NewPlayerRegister { get; set; } = new NewPlayerRegister();

public List<string> Names { get; set; } = default!;

public string Name { get; set; } = default!;

public IList<TblCountries> TblCountries { get; set; } = default!;
public void OnPost()
{
if (string.IsNullOrEmpty(this.Name))
{
ModelState.AddModelError(string.Empty, "Please select a country.");
Page();
return;
}
NewPlayerRegister.SetCountry(this.Name);
if (ModelState.IsValid)
{
// Process the form data and perform other necessary actions
string res = NewPlayerRegister.Name + "<br>" + NewPlayerRegister.Id + "<br>" + NewPlayerRegister.Phone + "<br>" + NewPlayerRegister.CountrySelection;


// Save data to the database using _context
// _context.NewPlayerRegisters.Add(NewPlayerRegister);
_context.SaveChangesAsync();

// or return any appropriate action result
this.Response.WriteAsync("<p>" + res + "</p>");
}
else
{
// Return to the page to display form validation error messages
Page();
}
}
}
3 replies
CC#
Created by IdoS | Computer Science on 7/7/2023 in #help
❔ recursive calls result in stackoverflow
Hi I have a connect four game logic that I implemented and I try to find the problem. I have a 6 x 7 array that is initialized with minus one for everyone on each turn I put the player's number where he chose to put say computer 0 player 1 the check is recursive in the form of an asterisk from where the player put the coin to check sequences of his coins. I understand it's because a bad halt check but I don't understand why is that wrong this is the code:
private int IsHorizontalWin(int x, int y, int playerFlag)
{
if (y < 0 || y >= IsFilled.GetLength(0) || x < 0 || x >= IsFilled.GetLength(1) || IsFilled[y, x] != playerFlag || IsFilled[y, x] == -1)
return 0;
return 1 + IsHorizontalWin(x + 1, y, playerFlag) + IsHorizontalWin(x - 1, y, playerFlag);
}
private int IsVerticalWin(int x, int y, int playerFlag)
{
if (y < 0 || y >= IsFilled.GetLength(0) || x < 0 || x >= IsFilled.GetLength(1) || IsFilled[y, x] != playerFlag || IsFilled[y, x] == -1)
return 0;
return 1 + IsVerticalWin(x, y - 1, playerFlag) + IsVerticalWin(x, y + 1, playerFlag);
}

private int IsDiagonalRightWin(int x, int y, int playerFlag)
{
if (y < 0 || y >= IsFilled.GetLength(0) || x < 0 || x >= IsFilled.GetLength(1) || IsFilled[y, x] != playerFlag || IsFilled[y, x] == -1)
return 0;
return 1 + IsDiagonalRightWin(x + 1, y - 1, playerFlag) + IsDiagonalRightWin(x - 1, y + 1, playerFlag);

}

private int IsDiagonalLeftWin(int x, int y, int playerFlag)
{
if (y < 0 || y >= IsFilled.GetLength(0) || x < 0 || x >= IsFilled.GetLength(1) || IsFilled[y, x] != playerFlag || IsFilled[y, x] == -1)
return 0;
return 1 + IsDiagonalLeftWin(x + 1, y + 1, playerFlag) + IsDiagonalLeftWin(x - 1, y - 1, playerFlag);

}
private int IsHorizontalWin(int x, int y, int playerFlag)
{
if (y < 0 || y >= IsFilled.GetLength(0) || x < 0 || x >= IsFilled.GetLength(1) || IsFilled[y, x] != playerFlag || IsFilled[y, x] == -1)
return 0;
return 1 + IsHorizontalWin(x + 1, y, playerFlag) + IsHorizontalWin(x - 1, y, playerFlag);
}
private int IsVerticalWin(int x, int y, int playerFlag)
{
if (y < 0 || y >= IsFilled.GetLength(0) || x < 0 || x >= IsFilled.GetLength(1) || IsFilled[y, x] != playerFlag || IsFilled[y, x] == -1)
return 0;
return 1 + IsVerticalWin(x, y - 1, playerFlag) + IsVerticalWin(x, y + 1, playerFlag);
}

private int IsDiagonalRightWin(int x, int y, int playerFlag)
{
if (y < 0 || y >= IsFilled.GetLength(0) || x < 0 || x >= IsFilled.GetLength(1) || IsFilled[y, x] != playerFlag || IsFilled[y, x] == -1)
return 0;
return 1 + IsDiagonalRightWin(x + 1, y - 1, playerFlag) + IsDiagonalRightWin(x - 1, y + 1, playerFlag);

}

private int IsDiagonalLeftWin(int x, int y, int playerFlag)
{
if (y < 0 || y >= IsFilled.GetLength(0) || x < 0 || x >= IsFilled.GetLength(1) || IsFilled[y, x] != playerFlag || IsFilled[y, x] == -1)
return 0;
return 1 + IsDiagonalLeftWin(x + 1, y + 1, playerFlag) + IsDiagonalLeftWin(x - 1, y - 1, playerFlag);

}
31 replies