1

I want to get and assign value to my textbox from controller.

here is my textbox:

<input type="text" class="form-control" id="RegardingTo" name="RegardingTo" value="??????"/>

then i want to get the value from this action.

    public ActionResult Edit(int? RequestID)
    {

        if (RequestID <= 0)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        var ReqID = db.usp_RequestGetDetails(RequestID);
        if (ReqID == null)
        {
            return HttpNotFound();
        }

        return View();
    }

please help :)

Jom Orolfo
  • 11
  • 6

2 Answers2

0

Yes you can assign value to the textbox using Model, First of all create a model then link that model with your view. And In Controller assign the value to model and return to the view.

Or At runtime if you want to assign the value to your text box then you can use Ajax call to your controller and get the value.

Please revert in case of any query.

0

See what i am doing to do the same, Make a custom model with your relevant fields, then assign values to them in controller, and pass this values to View, And that's it. :)

Custom Model

public partial class QuoteParameter
{
    public Nullable<System.DateTime> TripStartDateLimit { get; set; }
    public Nullable<System.DateTime> TripEndDateLimit { get; set; }     
    public int PolicyId { get; set; }
}

Controller

public ActionResult Index()
{            
    QuoteParameter quote = new QuoteParameter();
    quote.TripEndDateLimit = DateTime.Now;
    quote.TripEndDateLimit = DateTime.Now;
    quote.PolicyId = 5;
    return View(quote);                     
}    

View

@model EHIC.Models.Models.QuoteParameter

By Razor syntax

<div class="row-fluid span12">
      <div class="span4">
           <p><strong>Trip Start Date Limit :</strong></p>
      </div>
      <div class="span5">
            @Html.TextBoxFor(model => model.TripStartDateLimit, "{0:dd/MM/yyyy}", new { @class = "form-control", @placeholder = "Policy StartDate Limit", @required = true })
      </div>
</div>

<div class="row-fluid span12">
      <div class="span4">
           <p><strong>Trip End Date Limit :</strong></p>
      </div>
      <div class="span5">
           @Html.TextBoxFor(model => model.TripEndDateLimit, "{0:dd/MM/yyyy}", new { @class = "form-control", @placeholder = "Policy EndDate Limit", @required = true })
      </div>
</div>

By HTML Code

<input type="text" class="form-control" id="TripStartDateLimit" name="TripStartDateLimit" value="@Model.TripStartDateLimit"/>

<input type="text" class="form-control" id="TripEndDateLimit" name="TripEndDateLimit" value="@Model.TripEndDateLimit"/>

EDIT

By Click on this button you can send the PolicyId(as an example) to controller, and then you can do whatever you want there..!!!

<a href='../../controller/Edit?PolicyId=@Models.PolicyId'>
   <span title='Edit'></span>
</a>

@Html.ActionLink("Edit","Edit", new { id = item.RequestID })

You can find the PolicyId which you sent from the Edit Page..

public ActionResult Edit(int id)
{            
    //Get your data from Store_procedure..
    return View();                     
}
Smit Patel
  • 2,992
  • 1
  • 26
  • 44
  • thanks for your comment, i mean here is on the Edit action of my controller,I need the value of the RequestID to put all its value to the textbox. is this possible? i'm using a Stored procedure to select all the value of the said ID – Jom Orolfo Apr 14 '16 at 08:48
  • this is how I get the ID @Html.ActionLink("Edit","Edit", new { id = item.RequestID }) – Jom Orolfo Apr 14 '16 at 10:05
  • Yes, so change Controller Action to Get the id into it, in example change PolicyId to id – Smit Patel Apr 14 '16 at 10:38
  • By using this you can 100% get an id into controller edit method, if the type of `item.RequestID` is `int` – Smit Patel Apr 14 '16 at 10:45
  • Yes I got the ID by using this @Html.ActionLink("Edit","Edit", new { id = item.RequestID }) but i got this error The model item passed into the dictionary is of type 'System.Data.Entity.Core.Objects.ObjectResult`1[uRequestWeb.Models.usp_RequestGetDetails_Result]', but this dictionary requires a model item of type 'uRequestWeb.Models.usp_RequestGetDetails_Result'. – Jom Orolfo Apr 14 '16 at 11:03
  • Check with the Static code by Put `new { id = 2}` and let me know it will work or not? – Smit Patel Apr 14 '16 at 11:19
  • but when i try to insert this textbox: @Html.EditorFor(model => model.RegardingTo, new { htmlAttributes = new { @class = "form-control" } }) same error show.. – Jom Orolfo Apr 14 '16 at 11:56
  • Can you explain me your Flow?, What you need from Controller to View – Smit Patel Apr 14 '16 at 11:59
  • In index.cshtml, I have a table that data is from @foreach (var item in Model) then, it has an edit button at the end of every using this @Html.ActionLink("Edit","Edit", new { id = item.RequestID }). but in my edit.cshtml for, i want to use the storedProcedure to display the select ID values.that those values will be the value of every textbox in my edit.cshml form – Jom Orolfo Apr 14 '16 at 12:06
  • Check out this, http://stackoverflow.com/questions/20941017/mvc-net-edit-button-choose-right-method – Smit Patel Apr 14 '16 at 12:10