Click here to Skip to main content
15,887,333 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
1. this is my Controller
C#
[HttpGet]
public async Task GetCustomerMasterFile(int id)
{
	List CodeListing = new List();
	using (var httpClient = new HttpClient())
	{

		using (var response = await httpClient.GetAsync(
		    apiBaseUrl + "/GetCustomerMasterFileTbl?id=" + id))
		{
			string apiResponse = await response.Content.ReadAsStringAsync();
			CodeListing = JsonConvert.DeserializeObject>(apiResponse)!;
		}
	}
	var model = new GetCustomerMasterFileTbl
	{
		CustomerMasterFileJVM = CodeListing
	};

	foreach (var item in CodeListing)
	{
		model.CustomerIDno = item.CustomerIDno;
		model.LastName = item.LastName;
		model.FirstName = item.FirstName;
		model.MiddleName = item.MiddleName;
		model.BirthDate = item.BirthDate;

		model.Gender = item.Gender;
		model.CustomerAddress = item.CustomerAddress;
		model.ContactPerson = item.ContactPerson;
		model.ContactNumber = item.ContactNumber;
		model.CustomerEmail = item.CustomerEmail;

		model.CreatedBy = item.CreatedBy;
	}

	return View(model);
}

2. This is my data model
C#
public class GetCustomerMasterFileTbl
{
	public string? CustomerIDno { get; set; }
	public string? LastName { get; set; }
	public string? FirstName { get; set; }
	public string? MiddleName { get; set; }
	public DateTime? BirthDate { get; set; }
	public string? Gender { get; set; }
	public string? CustomerAddress { get; set; }
	public string? ContactPerson { get; set; }
	public string? ContactNumber { get; set; }
	public string? CustomerEmail { get; set; }
	public string? CreatedBy { get; set; }
	public DateTime DateCreated { get; set; }
	public string? Status { get; set; }
	public required List CustomerMasterFileJVM { get; set; }
}

3. this is my HTML Modal View
ASP.NET
@model CustomerService.Data.odel.AddCustomerMasterFileTbl

Customer Information
			Settings 1
			Settings 2

@using (Html.BeginForm("AddCustomerMasterFile",
                       "CustomerMasterFile",
 					   FormMethod.Post,
					   new { id = "addfrm" }))
{
    @Html.AntiForgeryToken()
	
    L-Name*
    @Html.TextBoxFor(m => m.LastName,
	                 new { @class = "form-control", required="required"})

	F-Name*
    @Html.TextBoxFor(m => m.FirstName,
                     new { @class = "form-control",  required="required"})

	Initial
    @Html.TextBoxFor(m => m.MiddleName,
	                 new { @class = "form-control", required="required"})

    B-Date *
    @Html.TextBoxFor(m => m.BirthDate,
                     new { @class = "form-control", placeholder="dd-mm-yyyy", required="required",   onfocus="this.type='date'",   onmouseover="this.type='date'", onclick="this.type='date'", onblur="this.type='text'" , onmouseout="timeFunctionLong(this)" })

    function timeFunctionLong(input) {
        setTimeout(function () {
            input.type = 'text';
        }, 60000);
    }

    Gender *
    @Html.DropDownListFor(model => model.Gender,
                          new List
						  {
						      new SelectListItem { Value = "M" , Text = "Male" },
							  new SelectListItem { Value = "F" , Text = "Female" }},
						  new { @class="form-control"})

    Address*
    @Html.TextBoxFor(m => m.CustomerAddress,
                     new { @class = "form-control",  required="required"})
		
    Reference*
    @Html.TextBoxFor(m => m.ContactPerson,
	                 new { @class = "form-control",  required="required"})

    Contact No*
    @Html.TextBoxFor(m => m.ContactNumber,
	                 new { @class = "form-control",  required="required"})

    Email*
    @Html.TextBoxFor(m => m.CustomerEmail,
	                 new { @class = "form-control",  required="required"})

    Created By*
    @Html.TextBoxFor(m => m.CreatedBy,
                     new { @class = "form-control", required="required"})

    Cancel
    Submit
}

@if (ViewBag.Message != null)
{
		window.onload = function () {
			alert("@ViewBag.Message");
		};
}

4. this is my way passing value to my controller
ASP.NET
@Html.ActionLink("Edit", "GetCustomerMasterFile",
                 new { id = @post.CustomerIDno },
				 new { @class = "btn btn-success btn-sm",
				 data_toggle = "modal",
				 data_target = "#new-edit-modal" })  
				 
@Html.ActionLink("Delete", "DeleteCustomer",
				 new { id = @post.CustomerIDno },
				 new { @class = "btn btn-primary btn-sm",
				 data_toggle = "modal",
				 data_target = "#new-delete-modal" })


What I have tried:

I tried to run the program client Edit no value passed to textbox
Posted
Updated 9-Jul-23 22:04pm
v3
Comments
Graeme_Grant 9-Jul-23 19:26pm    
This is you 5th question. Please learn to format your code in your questions. It appears that most of your html elements was lost, so impossible to answer. I have formatted what was there to show you how to post code correctly. Use the green "improve question" link on this question to fix your code.
Richard Deeming 10-Jul-23 3:54am    
As posted, your code won't compile; you can't return anything from an async Task method. The method would need to be async Task<Something> for that to work.

Beyond that, you've not explained what the problem is. Where is the code for the AddCustomerMasterFile action?
Graeme_Grant 10-Jul-23 4:52am    
I tried cleaning up his code however lots was missing. Only formatted to show how. Was waiting for him to come back and post the code correctly. I agree, more is missing than just code.

1 solution

There are a few issues in your code -
In your controller, make sure you are passing an instance of the 'GetCustomerMasterFileTbl' model to the view instead of 'AddCustomerMasterFileTbl'.
Replace the line -
@model CustomerService.Data.odel.AddCustomerMasterFileTbl
to -
@model GetCustomerMasterFileTbl

In your HTML Modal View, update the 'TextBoxFor' expressions to bind to the correct properties of the model -
HTML
L-Name*
@Html.TextBoxFor(m => m.LastName,
                 new { @class = "form-control", required = "required" })

F-Name*
@Html.TextBoxFor(m => m.FirstName,
                 new { @class = "form-control", required = "required" })

Initial
@Html.TextBoxFor(m => m.MiddleName,
                 new { @class = "form-control", required = "required" })

B-Date *
@Html.TextBoxFor(m => m.BirthDate,
                 "{0:yyyy-MM-dd}",
                 new { @class = "form-control", placeholder = "yyyy-MM-dd", required = "required", type = "date" })

In the controller action 'GetCustomerMasterFile', make sure you're returning the correct model -
C#
var model = new GetCustomerMasterFileTbl
{
    CustomerMasterFileJVM = CodeListing,
    CustomerIDno = CodeListing.FirstOrDefault()?.CustomerIDno,
    LastName = CodeListing.FirstOrDefault()?.LastName,
    FirstName = CodeListing.FirstOrDefault()?.FirstName,
    MiddleName = CodeListing.FirstOrDefault()?.MiddleName,
    BirthDate = CodeListing.FirstOrDefault()?.BirthDate,
    Gender = CodeListing.FirstOrDefault()?.Gender,
    CustomerAddress = CodeListing.FirstOrDefault()?.CustomerAddress,
    ContactPerson = CodeListing.FirstOrDefault()?.ContactPerson,
    ContactNumber = CodeListing.FirstOrDefault()?.ContactNumber,
    CustomerEmail = CodeListing.FirstOrDefault()?.CustomerEmail,
    CreatedBy = CodeListing.FirstOrDefault()?.CreatedBy
};

return View(model);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900