how can i assign value to textbox in MVC - Sharepoint Provider-hosted

84 Views Asked by At

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 :)

2

There are 2 best solutions below

1
On

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.

10
On

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/[email protected]'>
   <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();                     
}