3

enter image description here

if I'll click to add date begin_date and end_date will added to my view. At start I have only 1 begin and end date. dates are added using this:

 $(document).ready(function () {

    var str = '<div class="form-group">' +
            '@Html.LabelFor(model => model.begin_date, new { @class = "control-label col-md-2" })' +
            '<div class="col-md-10">' +
              '  @Html.TextBoxFor(model => model.begin_date, new { @Value = ViewBag.startDate, id = "begin_date" })' +
               ' @Html.ValidationMessageFor(model => model.begin_date)' +
            '</div>' +
    ' </div>' +

       ' <div class="form-group">' +
           ' @Html.LabelFor(model => model.end_date, new { @class = "control-label col-md-2" })' +
          '  <div class="col-md-10">' +
              '  @Html.TextBoxFor(model => model.end_date, new { id = "end_date", @Value = ViewBag.endDate })' +
             '   @Html.ValidationMessageFor(model => model.end_date)' +
          '  </div>' +
      '  </div>';

    $("#addItem").click(function () {
        $.ajax({
            url: this.href,
            cache: false,
            success: function (html) { $("#editorRows").append(str); }
        });
        return false;
    });
});

in my controller I have parameters on post DateTime begin_date, DateTime end_date and they're receiving only values of first begin_date and end_date. How can i receive value of first begin_date and last added end_date ?

my view: pastebin

post controller:

 [HttpPost]
    public ActionResult Create(int merchantId, string name, DateTime begin_date, DateTime end_date)
    {
ViewBag.startDate = DateTime.Now.AddDays(1).ToShortDateString();
        ViewBag.endDate = DateTime.Now.AddMonths(1).ToShortDateString();

        TmpUniPaySchedulesRowArray nameValuePairArray = new TmpUniPaySchedulesRowArray();
        nameValuePairArray.TmpUniPaySchedulesRowArrayObj = new TmpUniPaySchedulesRow[1];
        TmpUniPaySchedulesRow row = new TmpUniPaySchedulesRow();

        row.Id = 0;
        row.BeginDate = begin_date;
        row.EndDate = end_date;
        row.AmountPercentage = 100;

        nameValuePairArray.TmpUniPaySchedulesRowArrayObj[0] = row;
Oracle.DataAccess.Client.OracleCommand oracleCommand = new Oracle.DataAccess.Client.OracleCommand(); //gasasworebelia!!!!!

        oracleCommand.Connection = new Oracle.DataAccess.Client.OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["OracleBillUser"].ConnectionString);

        oracleCommand.CommandType = System.Data.CommandType.StoredProcedure;
        oracleCommand.CommandText = "tmp_uni.pay_schedules_group_add";
        oracleCommand.Parameters.Add(new Oracle.DataAccess.Client.OracleParameter("ai_merchant_id", Oracle.DataAccess.Client.OracleDbType.Int32, merchantId, System.Data.ParameterDirection.Input));
        oracleCommand.Parameters.Add(new Oracle.DataAccess.Client.OracleParameter("ai_name", Oracle.DataAccess.Client.OracleDbType.Varchar2, name, System.Data.ParameterDirection.Input));
        oracleCommand.Parameters.Add(new Oracle.DataAccess.Client.OracleParameter("ai_begin_date", Oracle.DataAccess.Client.OracleDbType.Date, begin_date, System.Data.ParameterDirection.Input));
        oracleCommand.Parameters.Add(new Oracle.DataAccess.Client.OracleParameter("ai_end_date", Oracle.DataAccess.Client.OracleDbType.Date, end_date, System.Data.ParameterDirection.Input));
        Oracle.DataAccess.Client.OracleParameter paramPayerInputData = new Oracle.DataAccess.Client.OracleParameter("ai_pay_schedules_values", Oracle.DataAccess.Client.OracleDbType.Array, nameValuePairArray, System.Data.ParameterDirection.Input);
        paramPayerInputData.UdtTypeName = "TMP_UNI_PAY_SCHEDULES_TYPE"; //case sensitive!!!
        oracleCommand.Parameters.Add(paramPayerInputData);
        oracleCommand.Parameters.Add(new Oracle.DataAccess.Client.OracleParameter("ao_error_code", Oracle.DataAccess.Client.OracleDbType.Int32, System.Data.ParameterDirection.Output));

        if (oracleCommand.Connection.State == System.Data.ConnectionState.Closed)
            oracleCommand.Connection.Open();

        try
        {
            oracleCommand.ExecuteNonQuery();

            int returnErrorCode = int.Parse(oracleCommand.Parameters["ao_error_code"].Value.ToString());
        }
        catch (OracleException ex)
        {
            Console.WriteLine(ex.ToString());
        }



        return RedirectToAction("Index");
    }
gsiradze
  • 4,055
  • 12
  • 49
  • 95
  • First begin_date and last end_date.... what are you going to do with the dates in between? – Alex Mar 04 '15 at 08:41
  • @Alex I need to send in database first and last date – gsiradze Mar 04 '15 at 08:42
  • Please can you post the method signature for your controller action method? – Alex Mar 04 '15 at 08:45
  • 1
    Your duplicating control names. Your post method needs to accept parameter `IEnumerable` and you need to construct the html `name` attributes correctly with indexers –  Mar 04 '15 at 08:46
  • @StephenMuecke can u give me an example? – gsiradze Mar 04 '15 at 08:48
  • @George, Not exactly sure what you are trying to do here. Looking at the last edit you seem to only want to post back one item (is that correct). You have multiple problems with the `var `str =...` because your generating invalid html as well as duplicate name attributes. –  Mar 04 '15 at 08:54

1 Answers1

2

The model binding uses name of your inputs; Since you're duplicating the control names, try changing your method to accept a collection of date time objects:

public ActionResult Create(int merchantId, string name, IEnumerable<DateTime> begin_date, IEnumerable<DateTime> end_date)

You will of course have to traverse this to get the first / last item yourself.

var firstBeginDate = begin_date.First();
var lastEndDate = end_date.Last();
Alex
  • 34,123
  • 47
  • 189
  • 315