0

I have a razor view in which a person's information are displayed, it route is https://localhost:PORT/Pesron/Details/E-12345.

I want to create a profile for the said person using another controller, the corresponding link would be https://localhost:PORT/Profile/Create/E-12345.

The page would open normally, but I would like getting the value E-123456 which is the person's ID and obviously changes from one to another, into a (disabled) input box in Create, so when I fill other information and press submit, a new profile is created, I am doing it using MVC controller but wouldn't mind using Web API too. How I open Create in Person's Details view.

<a asp-action="Create" asp-controller="Profile" asp-route-id="@Model.ID">Create</a>

My 2 Create action methods.

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(string MRN, [Bind("Id,Dep_Id,Check_In,Check_Out")] Profile profile)
        {
            ViewData["ID"] = MRN;


            if (ModelState.IsValid)
            {
                _context.Add(profile);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }


            return View(profile);
        }

        public IActionResult Create(string MRN)
        {
           ViewData["ID"] = MRN;

            return View();
        }

Could the reason be that the value I am sending is not the primary key, I made it hidden and auto incrementing.

5
  • Hi ,any update about this case?If it doesn't work,you can provide your code. Commented May 10, 2021 at 7:26
  • You should use asp-route-MRN="@Model.ID". Change your code to <a asp-action="Create" asp-controller="Profile" asp-route-MRN="@Model.ID">Create</a> Commented May 10, 2021 at 7:55
  • Does putting MRN in the Bind() have anything to do with it? Commented May 10, 2021 at 8:08
  • You don't need to put MRN in the Bind().You can have a try. Commented May 10, 2021 at 8:09
  • Removing Bind() is solving the issue Commented May 10, 2021 at 8:44

1 Answer 1

1

You can use ViewData.Below is an example.

View:

<a asp-action="Create" asp-controller="Profile" asp-route-id="@Model.ID">Create</a>

Create action:

 public IActionResult Create(string id)
    {
        ViewData["ID"] = id;

        return View();
    }

Create View:

 <input value="@ViewBag.ID" disabled="disabled"/>

Post Create method:

 [HttpPost]
    public IActionResult Create(Profile profile, string id)
    {
        //.....
        return RedirectToAction("actionname","Controllername");
    }
 
Sign up to request clarification or add additional context in comments.

5 Comments

Not working, also trying @ViewData["ID"], not working too.
Where does it not work? Is the correct id not displayed in the input box?
Yes, the input box is blank.
Hit a breakpoint in your Create action, observe whether the id is successfully bound, and make sure that <input value="@ViewBag.ID" disabled="disabled"/> is in your Profile/Create view.
You can see my test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.